Is the following code using if ... and ...:
safe, i.e. are we sure the first condition is tested first and that, if the key is not present, the second condition is ignored?
d = {'a': 1}
if 'b' in d and d['b'] == 2:
print 'hello'
It seems that yes, because this generates no error. But is it true for all Python versions?
Will it never generate a KeyError: 'b'
?
PS: this is probably more pythonic, with no ambiguity:
if 'b' in d:
if d['b'] == 2:
...