__contains__ vs. __getitem__

Pedro Werneck pedro.werneck at terra.com.br
Wed Aug 9 14:19:17 EDT 2006


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.


-- 
Pedro Werneck



More information about the Python-list mailing list