equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)
A.T.Hofkamp
hat at se-162.se.wtb.tue.nl
Thu Jun 28 10:38:56 EDT 2007
On 2007-06-28, Alan Isaac <aisaac at american.edu> wrote:
> A.T.Hofkamp wrote:
>
>>>>>a = Car2(123)
>>>>>b = Car2(123)
>>>>>a == b
>>
>> True
>>
>>>>>set([a,b])
>>
>> set([Car2(123), Car2(123)])
>>
>> I get a set with two equal cars, something that never happens with a set
>> my math teacher once told me.
>
>
> Then your math teacher misspoke.
> You have two different cars in the set,
> just as expected. Use `is`.
> http://docs.python.org/ref/comparisons.html
>
> This is good behavior.
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 "one should override __hash__ anyway" argument is being discussed in my
previous post.
Albert
More information about the Python-list
mailing list