19
votes

This is a possibly silly question, but looking at the mapping of operators to functions I noticed that there is no function to express the not in operator. At first I thought this was probably because the interpreter just reorders this to be not x in y, but there is a function for is not which seems like it should behave exactly the same as not in. Am I missing something, or does that operator really not exist?

Here's a really stupid example where you might want this:

def compare_iter(a,b,func):
    return [func(aa,bb) for aa,bb in zip(a,b)]

my_compare=compare_iter(xx,yy,lambda x,y:x not in y)  #lambda -- yuck
my_compare=map(operator.not_,compare_iter(xx,yy,operator.contains)  #extra map?  grr...
#it would be nice to do: my_compare=compare_iter(xx,yy,operator.not_contains)

Of course I could write my own function for this, but then you pay a price in efficiency whereas the operator module could push this code out of python and therefore execute faster.

2
Indeed, couldn't a is not b simply be reordered to not a is b? - JAB
Are there situation where a check for not in could potentially be done faster than a check for in? - Paul Manta
@PaulManta doubtful, since not in is by definition an exhaustive search. - Colin Dunklau
@JAB -- Yes it can. But that's not the point. The point is that there is an asymmetry here which is a little funny. It seems like there should be an not_contains function which could then be used to pass to other functions or whatever (instead of relying on lambda). - mgilson
@ColinDunklau Not necessarily. Think about things like Bloom Filters where if but one bit is not set, you can be sure the item was not inserted into the set. - Paul Manta

2 Answers

15
votes

Another function is not necessary here. not in is the inverse of in, so you have the following mappings:

obj in seq => contains(seq, obj)

obj not in seq => not contains(seq, obj)

You are right this is not consistent with is/is not, since identity tests should be symmetrical. This might be a design artifact.

4
votes

You may find the following function and disassembly to be helpful for understanding the operators:

>>> def test():
        if 0 in (): pass
        if 0 not in (): pass
        if 0 is (): pass
        if 0 is not (): pass
        return None

>>> dis.dis(test)
  2           0 LOAD_CONST               1 (0) 
              3 LOAD_CONST               2 (()) 
              6 COMPARE_OP               6 (in) 
              9 POP_JUMP_IF_FALSE       15 
             12 JUMP_FORWARD             0 (to 15) 

  3     >>   15 LOAD_CONST               1 (0) 
             18 LOAD_CONST               3 (()) 
             21 COMPARE_OP               7 (not in) 
             24 POP_JUMP_IF_FALSE       30 
             27 JUMP_FORWARD             0 (to 30) 

  4     >>   30 LOAD_CONST               1 (0) 
             33 LOAD_CONST               4 (()) 
             36 COMPARE_OP               8 (is) 
             39 POP_JUMP_IF_FALSE       45 
             42 JUMP_FORWARD             0 (to 45) 

  5     >>   45 LOAD_CONST               1 (0) 
             48 LOAD_CONST               5 (()) 
             51 COMPARE_OP               9 (is not) 
             54 POP_JUMP_IF_FALSE       60 
             57 JUMP_FORWARD             0 (to 60) 

  6     >>   60 LOAD_CONST               0 (None) 
             63 RETURN_VALUE         
>>> 

As you can see, there is a difference in each operator; and their codes (in order) are 6, 7, 8, and 9.