wtfpython
wtfpython copied to clipboard
Weird variable scope in functions
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