4 ms·
Not the OP, but I think what they are referring to is the following: def foo(): return bar if ...: bar = 42 print(foo()) else: print(f
by Cu3PO42 8y ago
Not the OP, but I think what they are referring to is the following:
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.