2

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:
        ...
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Yes. Both `and` and `or` are _guaranteed_ by the language to short-circuit (as I illustrate in [this answer](http://stackoverflow.com/a/36551857/4014959)), it's not just an implementation detail. Also note that the `all` and `any` functions short-circuit; to get the full benefit of that behaviour you should feed them generators. – PM 2Ring Dec 02 '16 at 10:54
  • More pythonic way, you can do `if d.get('b', None) == 2:`, it will get None if key b not exist – Skycc Dec 02 '16 at 10:58
  • 1
    @Skycc if you want the default value to be `None` from `get()` then it should be `if d.get('b')` since this will automatically return `None` if it's not found. The second argument is for defining a custom default value. – roganjosh Dec 02 '16 at 11:00
  • @Skycc And in Python 3 (or Python 2 with `from __future__ import print_function`) you could eliminate the `if` and just do `d.get('b') == 2 and print('hello')` although some people will object to that style on readability grounds. – PM 2Ring Dec 02 '16 at 11:01
  • @PM2Ring : I would **never** have found [the question you marked as duplicate](http://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) because I didn't know the right keyboard was "shortcircuiting". Already knowing this keyword is equivalent to knowing the answer because then it's straightforward to find in the doc, so I find it a bit hard to see this as a "duplicate" ;) Moreover, this duplicate question is pretty obscure (no example of what shortcircuiting is in the question...) – Basj Dec 02 '16 at 11:03
  • @roganjosh , you are correct, i usually default to some value other than None and not aware its default to return None – Skycc Dec 02 '16 at 11:04
  • @PM2Ring agree with you on that, but its limit to statement, i always thought of converting `if a==5:b=10` into `a==5 and b=10` but i can't do that with assignment operator – Skycc Dec 02 '16 at 11:09
  • @Basj I agree that it's not easy to find that linked question if you aren't familiar with the term "short-circuiting". But now you know. :) There's certainly nothing wrong with asking a duplicate question when you don't know the correct terminology. And there's nothing wrong with asking a good duplicate question, since that may help future readers who don't know the right terminology either. – PM 2Ring Dec 02 '16 at 11:33
  • We don't close duplicate questions because they're intrinsically bad: they can be very useful signposts, so the system creates links in both directions between the new question and the "dupe target". But we like to close them ASAP because we want to make it easy to find the relevant answers. By having all of those answers on one page (or a small number of pages) that makes it easier for future readers to find those answers and to judge their relative merits via the votes they've been given. – PM 2Ring Dec 02 '16 at 11:43

1 Answers1

2

Yes, it is called a short circuit and.

Both python2 (Link to the doc) and python3.x(Link to the doc) support short circuit and and or

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52