[issue21234] __contains__ and friends should check "is" for all elements first
Jurjen N.E. Bos
report at bugs.python.org
Tue Apr 15 21:00:16 CEST 2014
Jurjen N.E. Bos added the comment:
Well, I partially agree. I see the following points:
Against my proposal:
- For *very* big containers, it can be slower in the case the object is early in the container, as you pointed out.
- Current behaviour is easier to understand.
OTOH, fore the proposal:
- Such giant containers are not efficient anyway; that's were set/Counter can help.
- The documentation doesn't promise anywhere the objects are scanned in order.
Anyway, if this is supposed to be the behaviour, I suggest to document it, and add the following recipe for people dealing with the same problem as I had:
from operator import ne
from itertools import repeat
class MyContainer:
"""container allowing equality search with containsEqual,
while allowing fast identity search with __contains__:
use "obj in c" to test if obj exactly sits in c
use "c.containsEqual(obj)" to test if an object in c has c==obj
"""
def containsEqual(self, object):
return not all(map(ne, zip(repeat(object), self)))
def __ne__(self, object):
"Your not-equal test"
If you see a more elegant equivalent recipe, feel free to add.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21234>
_______________________________________
More information about the Python-bugs-list
mailing list