Contains/equals

Karsten Wutzke kwutzke at web.de
Thu Aug 19 10:53:32 EDT 2010


On Aug 19, 4:47 pm, Peter Otten <__pete... at web.de> wrote:
> 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

Whoops that easy... Thanks!
Karsten



More information about the Python-list mailing list