equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)

Alan Isaac aisaac at american.edu
Mon Jul 2 04:23:10 CEST 2007


A.T.Hofkamp wrote:
> Hmm, maybe numbers in sets are broken then?
>>>>a = 12345
>>>>b = 12345
>>>>a == b
> 
> True
> 
>>>>a is b
> 
> False
> 
>>>>set([a,b])
> 
> set([12345])
> 
> Numbers and my Car2 objects behave the same w.r.t. '==' and 'is', yet I get a
> set with 1 number, and a set with 2 cars.
> Something is wrong here imho.
> 
> The point I intended to make was that having a default __hash__ method on
> objects give weird results that not everybody may be aware of.
> In addition, to get useful behavior of objects in sets one should override
> __hash__ anyway, so what is the point of having a default object.__hash__ ?


The point is: let us have good default behavior.
Generally, two equal numbers are two conceptual
references to the same "thing".  (Say, the Platonic
form of the number.) So it is good that the hash value
is determined by the number.  Similarly for strings.
Two equal numbers or strings are **also** identical,
in the sense of having the same conceptual reference.
In contrast, two "equal" cars are generally not identical
in this sense.  Of course you can make them so if you wish,
but it is odd.  So *nothing* is wrong here, imo.

Btw:
>>> a = 12
>>> b = 12
>>> a == b
True
>>> a is b
True

Cheers,
Alan Isaac



More information about the Python-list mailing list