Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 'not e in c' differ from 'e not in c' in Python? [duplicate]

Tags:

python

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?

like image 437
szablica Avatar asked Oct 17 '12 18:10

szablica


2 Answers

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  
like image 181
Ashwini Chaudhary Avatar answered Oct 02 '22 03:10

Ashwini Chaudhary


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.

like image 32
mipadi Avatar answered Oct 02 '22 03:10

mipadi