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

Steve Holden steve at holdenweb.com
Thu Jun 28 21:10:03 EDT 2007


A.T.Hofkamp wrote:
> 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.

Hmm, I suspect you'll like this even less:

 >>> set((1.0, 1, 1+0j))
set([1.0])

Just the same there are sound reasons for it, so I'd prefer to see you 
using "counterintuitive" or "difficult to fathom" rather than "broken" 
and "wrong".

Such language implies you have thought about this more deeply than the 
developers (which I frankly doubt) and that they made an inappropriate 
decision (which is less unlikely, but which in the case you mention I 
also rather doubt).

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list