[Python-Dev] misbehaving contains (original) (raw)
Raymond Hettinger python at rcn.com
Wed Jan 23 02:19:08 CET 2008
- Previous message: [Python-Dev] misbehaving __contains__
- Next message: [Python-Dev] misbehaving __contains__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
And, would we lose the nice relationship expressed by:
for elem in container: assert elem in container
[Steven Bethard]
We've already lost this if anyone really wants to break it::
>>> class C(object): ... def iter(self): ... return iter(xrange(3)) ... def contains(self, item): ... return False
I think this is in the same category as writing a hash that doens't match eq. It is explicitly shooting yourself in the foot.
I'm more concerned about introducing an API where well-meaning attempts to code a contains override will implicitly shoot the toes off of client code that innocently assumes a somewhat sensible invariant relation between looping over elements in a container and those elements being in the container. The code for sets and frozensets makes that assumption, for example. And, when the code does break, the errors will be darned hard to find.
Raymond
- Previous message: [Python-Dev] misbehaving __contains__
- Next message: [Python-Dev] misbehaving __contains__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]