Contains/equals

Christian Heimes lists at cheimes.de
Thu Aug 19 11:27:05 EDT 2010


Am 19.08.2010 17:00, schrieb Alex Hall:
> In Python, as I understand it, you can define this behavior.
> 
> class c(object):
>  def __init__(self, a=1, b=2):
>   self.a=a; self.b=b
> 
>  def __eq__(self, obj):
>   if self.a==obj.a and self.b==obj.b: return True
>   return False

Yes, but you have to return NotImplemented when your type doesn't know
how to handle the other type.

class C(object):
    def __eq__(self, other):
        if not isinstance(other, C):
            return NotImplemented
        return self.a == other.a

Christian




More information about the Python-list mailing list