Contains/equals
Tim Chase
python.list at tim.thechases.com
Thu Aug 19 14:53:36 EDT 2010
On 08/19/10 12:42, Steven D'Aprano wrote:
> On Thu, 19 Aug 2010 11:00:03 -0400, Alex Hall wrote:
>
>> def __eq__(self, obj):
>> if self.a==obj.a and self.b==obj.b: return True
>> return False
>
> That would be the same as:
>
> def __eq__(self, obj):
> return self.a==obj.a and self.b==obj.b
Or, if you have lots of attributes and 2.5+
def __eq__(self, other):
return all(
getattr(self, attr) == getattr(other, attr)
for attr in ['a', 'b', 'c', ...]
)
or even something like
def __eq__(self, other):
return all(
getattr(self, attr) == getattr(other, attr)
for attr in dir(self)
if not attr.startswith("__") and not attr.endswith("__")
)
-tkc
More information about the Python-list
mailing list