19

Possible Duplicate:
Order of syntax for using ‘not’ and ‘in’ keywords

My TA claims that e not in c doesn't always yield the same results as not e in c (he didn't give an explanation why though). I've never personally seen the second form in anyone's code (except his and books explaining that the two are equivalent) and never seen the two to differ in behavior so I'm suspicious of this claim. Not having found anything on it through Google I decided to come here.

So does anyone have any information about any case in which the behavior of the two differs?

Community
  • 1
  • 1
szablica
  • 195
  • 5
  • There's no difference in py2k – inspectorG4dget Oct 17 '12 at 18:54
  • Do you imply that there is one in Python 3.x? – szablica Oct 17 '12 at 18:56
  • 2
    I don't know about py3k. I know that there is no difference in py2k – inspectorG4dget Oct 17 '12 at 19:00
  • 5
    there is 100% proof in the answer below ... take it to your TA and tell him he is wrong! ... but tell him nicely ... – Joran Beasley Oct 17 '12 at 19:02
  • 2
    There is a difference: `not e in c` yields extra confusion. If I saw it in code, I'd have to look up the operator precedence to see it wasn't `(not e) in c`. – Fred Foo Oct 17 '12 at 19:03
  • if e is a boolean, (not e) will be flipped and that changes the meaning. – ronak Oct 17 '12 at 19:04
  • They're the same, I asked a [related question](http://stackoverflow.com/q/11435206/748858) a little while back. The fact that they are the same is (in my opinion) an oversight in the specification ... Perhaps I should take it up with Guido if I ever meet him ... – mgilson Oct 17 '12 at 19:06
  • @ronak: wrong. The language is designed so that both are in fact equivalent, and this takes place due to operator precedence. – jsbueno Oct 17 '12 at 19:36
  • see Ben's answer here -> http://stackoverflow.com/a/8738649/674039 – wim Oct 18 '12 at 01:04

2 Answers2

26

They are exactly same, as both actually apply the not in comparision:

In [25]: def func():
    'e' not in 'bee'
   ....:     
   ....:     

In [27]: def func1():
    not 'e' in 'bee'
   ....:     
   ....:     
In [29]: dis.dis(func)
  2           0 LOAD_CONST               1 ('e')
              3 LOAD_CONST               2 ('bee')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        

In [30]: dis.dis(func1)
  2           0 LOAD_CONST               1 ('e')
              3 LOAD_CONST               2 ('bee')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE  
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

They're identical. not has a lower precedence than in, so not x in y is parsed as not (x in y), which returns the opposite of in, which is what not in does.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • if the negation happens seperately, how come Ashwini's answer shows that it yields the same bytecode? is it in optimization? – Janus Troelsen Oct 17 '12 at 19:04
  • Even explicitly adding the parens doesn't affect the bytecode generated. The block of code won't be formatted in a comment though. Still, I believe @mipadi means how it works semantically, and not the actual implementation. – szablica Oct 17 '12 at 19:13
  • 1
    @JanusTroelsen: Yeah, as pointed out, I'm showing the semantics of the code, not what's actually generated (which might be dependent on implementation details). – mipadi Oct 17 '12 at 19:34