Contains/equals

Peter Otten __peter__ at web.de
Thu Aug 19 10:47:46 EDT 2010


Karsten Wutzke wrote:

> I have an object which has a list of other complex objects. How do I
> best achieve a complex "contains" comparison based on the object's
> class? In Java terms, I'm looking for an equivalent to equals(Object)
> in Python. Does a similar thing exist? Directions appreciated.

I can't infer from your question whether you already know about 
__contains__(). So there:

>>> class Cornucopia:
...     def __contains__(self, other):
...             return True
...
>>> c = Cornucopia()
>>> 42 in c
True
>>> "cherry pie" in c
True
>>> c in c
True




More information about the Python-list mailing list