__contains__ vs. __getitem__

David Isaac aisaac0 at verizon.net
Wed Aug 9 12:51:23 EDT 2006


I have a subclass of dict where __getitem__ returns None rather than
raising KeyError for missing keys.  (The why of that is not important for
this question.)

I was delighted to find that __contains__ still works as before
after overriding __getitem__.    So even though instance['key']
does not raise KeyError, I still get (as desired)  'key' in instance ==
False.

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.

Thank you,
Alan Isaac





More information about the Python-list mailing list