> If you call a function that happens to refer to a variable that only exists in the calling code, it will work
Could you give an example of what you mean by this?
Could you give an example of what you mean by this?
def foo():
return bar
if ...:
bar = 42
print(foo())
else:
print(foo())
If ... evaluates to a truthy value, bar will be defined and foo() will return 42, if it is falsy, bar will not be defined and the code will throw a NameError.I would agree that this is extremely confusing behaviour. Of course people wouldn't write code like this (hopefully), but a similar situation might occur in a much more obscure situation.