wtfpython icon indicating copy to clipboard operation
wtfpython copied to clipboard

Weird variable scope in functions

Open dan-t opened this issue 4 years ago • 0 comments

def foo(x):
    if x:
        y = 1
    print(f"y={y}")

foo(x=True)
y=1

foo(x=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in foo
UnboundLocalError: local variable 'y' referenced before assignment

dan-t avatar Feb 11 '21 11:02 dan-t