__eq__ and sets

Chris Reedy creedy at mitretek.org
Wed Feb 26 15:41:27 CET 2003

Tim Peters wrote:
> [Chris Reedy]
>>I recently encountered the following:
>> >>> from sets import Set
>> >>> x = Set([1])
>> >>> x == 2
>>TypeError: Binary operation only permitted between sets
>>The TypeError is raised by Set.__eq__ (actually BaseSet.__eq__) upon
>>recognizing that 2 is not a set.
>>My question: Is this the expected and/or appropriate behavior?
> It's the implemented behavior <wink>.

Well, yes <wink, wink>

>>In the case in question, I was expecting false to be returned. Changing
>>the __eq__ and __ne__ operators in sets.py to return NotImplemented
>>instead or raising an exception would result in the behavior I was
> No, it wouldn't.  If, when you compare x to y, both objects return
> NotImplemented, Python automatically falls back to comparing x and y by
> their memory addresses (cmp(id(x), id(y))).  The Set class definitely didn't
> want you to get a nonsense result from "x == 2", and, in the absence of a
> meaningful result, the only way to stop the comparison machinery from
> falling back to address comparison is to raise an exception.
> The objects in the new (for 2.3) datetime module used to raise an exception
> on mixed-type comparisons too.  Between 2.3a1 and 2.3a2, I changed them so
> that mixed-type __eq__ comparison returns False, and mixed-type __ne__
> comparison returns True.  This is discussed in the 2.3a2 NEWS file, and I
> think that may be a useful strategy for other types of objects that don't
> want to fall back to address comparison.  It allows things like "x in
> sequence" to work without raising pointless TypeErrors.

I think the behavior I'd like to see is what's described here. The code 
that caused me to ask the question was in testing logic where I wanted 
to know whether the result was what was expected. In this case I wanted 
a result of False for incomparable mixed-types.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

More information about the Python-list mailing list