Newbie Question - Overloading ==

Bruno Desthuilliers bruno.42.desthuilliers at
Tue Apr 1 11:02:27 CEST 2008

Duncan Booth a écrit :
> "bruno.desthuilliers at" <bruno.desthuilliers at> wrote:
>>> Surely an A isn't equal to every other object which just happens to
>>> have the same attributes 'a' and 'b'?
>> And why not ?-)
>>> I would have thoughts the tests want to be
>>> something like:
>>> class A:
>>>     def __eq__(self,other):
>>>          return (isinstance(other, A) and
>>>             self.a == other.a and self.b == other.b)
>>> (and similar for B) with either an isinstance or exact match required
>>> for the type.
>> I don't think there's a clear rule here. Python is dynamically typed
>> for good reasons, and MHO is that you should not fight against this
>> unless you have equally good reasons to do so.
> I fully agree with that, but an apple != a pear, even if they are the same 
> size and colour. 

It mostly depends on the problem at hand. It may be that for some 
problem, an apple == a pear if they have the same size and colour.

> There will be some types where you can have equality 
> between objects of different types (e.g. int/float), but more often the 
> fact that they are different types wil automatically mean they are not 
> equal.

'most often' != 'always' !-)

More information about the Python-list mailing list