__contains__ vs. __getitem__
Alex Martelli
aleax at mac.com
Fri Aug 11 01:45:05 EDT 2006
Pedro Werneck <pedro.werneck at terra.com.br> wrote:
> On Wed, 09 Aug 2006 16:51:23 GMT
> "David Isaac" <aisaac0 at verizon.net> wrote:
>
> > Looking forward:
> > Can I count on this independence of __getitem__ and __contains__?
> > I would like to understand whether it will be safe to count on this
> > behavior.
>
> With the builtin 'dict' implementation, dict.__contains__() use the
> dict_has_key() C function, and does not touch your subclass __getitem__
> python method. I don't think it can be called 'safe'... it may lead to
> very inconsistent behavior. It's like overridind both __eq__ and __ge__
> with a different behavior. It's better for you to override
> __contains__() too.
I think it's perfectly safe -- that's what defaultdict in Python 2.5
does, after all.
Alex
More information about the Python-list
mailing list