This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author nparikh
Recipients docs@python, nparikh
Date 2012-09-24.00:13:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1348445631.47.0.961273834435.issue16011@psf.upfronthosting.co.za>
In-reply-to
Content
The Python 2.7.3 documentation says the following about defining __contains__:

"Called to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs."

This suggests that while you should return True/False, you don't need to. Also, the documentation doesn't say that if you return a value other than True or False, it will silently coerce other values to be True or False when you use "in". For example:

>>> class Foo(object):
...     def __contains__(self, item): return 42
... 
>>> foo = Foo()
>>> 3 in foo
True
>>> foo.__contains__(3)
42

When __contains__ is defined, "in" should return whatever __contains__ returns, even if this value is neither True nor False. That would be consistent with, for example, the comparison operators: You can return anything from __lt__ and "x < 4" will correctly pass along that value regardless of what it is.
History
Date User Action Args
2012-09-24 00:13:51nparikhsetrecipients: + nparikh, docs@python
2012-09-24 00:13:51nparikhsetmessageid: <1348445631.47.0.961273834435.issue16011@psf.upfronthosting.co.za>
2012-09-24 00:13:50nparikhlinkissue16011 messages
2012-09-24 00:13:49nparikhcreate