3

My question is fairly simple, and surprisingly, I have not been able to find an answer to it:

Is there any scenario under which x not in y and not x in y yield different results?

What about the case where x and/or y are more complicated expressions of some sort?

Could operator-precedence cause each of the above expressions to yield a different result?

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • **No**. The second one is actually [converted](http://hg.python.org/cpython/file/ed8b0ee1c531/Python/peephole.c#l407) to the first expression by the peephole optimizer. – Ashwini Chaudhary Aug 07 '14 at 08:12
  • @AshwiniChaudhary: I've slightly updated the question, can you please have a second look? – barak manos Aug 07 '14 at 08:15
  • @barakmanos That's the case for any `x` and `y` regardless of their type. – Maroun Aug 07 '14 at 08:17
  • @MarounMaroun: I'm not talking about types. Will there be a problem if I use, for example, `a or b not in c` vs. `not a or b in c`? – barak manos Aug 07 '14 at 08:20
  • @AshwiniChaudhary: In addition, this is not a question about which one is more Pythonic. It's a question about whether or not an actual scenario exists, under which the above two will yield different results. – barak manos Aug 07 '14 at 08:23
  • @barakmanos In this case there might be differences, I voted to reopen the question. – Maroun Aug 07 '14 at 08:26
  • @barakmanos Counterexample: `True or False not in [True,False]` and `not True or False not in [True, False]`. – Maroun Aug 07 '14 at 08:32
  • @MarounMaroun: There's an extra `not` in your second expression. If that's a typo, then - nice catch, and I would give you the V for it (though as you can see it is currently not possible). – barak manos Aug 07 '14 at 08:35
  • @barakmanos Sorry.. Didn't note that. But here's another one: `False or True not in [True,False]` VS `not False or True in [True, False]` – Maroun Aug 07 '14 at 08:44
  • @MarounMaroun: Like I said, get another couple of users to reopen this question (if you really think that it's eligible to be reopened), and I'll give you the Accept :) – barak manos Aug 07 '14 at 08:48

0 Answers0