Feature Request: `operator.not_in`

Matthew Gilson m.gilson1 at gmail.com
Fri Apr 19 10:27:09 EDT 2013


I believe that I read somewhere that this is the place to start 
discussions on feature requests, etc.  Please let me know if this isn't 
the appropriate venue (and what the appropriate venue would be if you know).

This request has 2 related parts, but I think they can be considered 
seperately:

1) It seems to me that the operator module should have a `not_in` or 
`not_contains` function.  It seems asymmetric that there exists a 
`is_not` function which implements `x is not y` but there isn't a 
function to represent `x not in y`.

2) I suspect this one might be a little more controversial, but it seems 
to me that there should be a separate magic method bound to the `not in` 
operator.  Currently, when inspecting the bytecode, it appears to me 
that `not x in y` is translated to `x not in y` (this supports item 1 
slightly).  However, I don't believe this should be the case.  In 
python, `x < y` does not imply `not x >= y` because a custom object can 
do whatever it wants with `__ge__` and `__lt__` -- They don't have to 
fit the normal mathematical definitions.  I don't see any reason why 
containment should behave differently.  `x in y` shouldn't necessarily 
imply `not x not in y`.  I'm not sure if `object` could have a default 
`__not_contains__` method (or whatever name seems most appropriate) 
implemented equivalently to:

      def __not_contains__(self,other):
           return not self.__contains__(other)

If not, it could probably be provided by something like 
`functools.total_ordering`.  Anyway, it's food for thought and I'm 
interested to see if anyone else feels the same way that I do.

Thanks,
~Matt



More information about the Python-list mailing list