19
votes

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?

2
There's no difference in py2k - inspectorG4dget
Do you imply that there is one in Python 3.x? - szablica
I don't know about py3k. I know that there is no difference in py2k - inspectorG4dget
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
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

2 Answers

26
votes

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  
2
votes

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.