I was surprised today to see that the following works with no exceptions (in Python 2.7.3 at least):
>>> try:
... pass
... except ThingThatDoesNotExist:
... print "bad"
...
>>>
I would have thought that this should raise a NameError
in the REPL, similar to how the following would:
>>> x = ThingThatDoesNotExist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ThingThatDoesNotExist' is not defined
Anyone have any idea what's going on here?
The same reason this does not raise a exception:
>>> True or ThingThatDoesNotExist
Python looks up names exactly the moment they need to be evaluated. Names that don't need to be evaluated are not looked up and it's the failed lookup that raises the exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With