4 ms·
> 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 t
by kbp 8y ago
> 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?
- Cu3PO42 8y agoNot 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.