[Python-bugs-list] [ python-Bugs-693121 ] Set == non-Set is a TypeError
SourceForge.net
noreply@sourceforge.net
Sat, 01 Mar 2003 16:32:24 -0800
Bugs item #693121, was opened at 2003-02-25 14:37
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=693121&group_id=5470
Category: Python Library
Group: Python 2.3
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Chris Reedy (creedy)
Assigned to: Tim Peters (tim_one)
Summary: Set == non-Set is a TypeError
Initial Comment:
Comparing Sets to non-Sets results in a TypeError. For
example:
>>> from sets import Set
>>> x = Set([1])
>>> x == 2
TypeError: Binary operation only permitted between sets
This seems to be inconsistent with other Python
behavior. For example:
>>> (1,2,3) == 2
0
>>> "abcd" == 2
0
Assuming that the standard behavior is what is desired,
the implementation of __eq__ and other comparison
operators in sets.py should be changed to return
NotImplemented when the other object is not a Set.
Note: Looking at the code, I'm not sure whether the
implementation of __lt__, __le__, etc. should also be
changed to not return a Type Error.
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2003-03-01 19:32
Message:
Logged In: YES
user_id=31435
This turned out to be messier than I hoped, because Set
also implements __cmp__, so returning NotImplented
didn't do a lick of good. See the checkin msg and new code
comments for discussions of subtleties.
Lib/sets.py; new revision: 1.43
Checking in Lib/test/test_sets.py; new revision: 1.21
Misc/NEWS; new revision: 1.685
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-03-01 13:02
Message:
Logged In: YES
user_id=80475
That makes sense. What I don't understand is how to
implement it. Any change I make to __eq__ and __ne__
causes test_cmp to fail.
The new tests are attached.
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2003-02-28 21:48
Message:
Logged In: YES
user_id=6380
Yes, that's the right solution. In general, more objects
should implement this behavior: __eq__, __ne__ return
NotImplemented, the others continue to raise TypeError.
----------------------------------------------------------------------
Comment By: Tim Peters (tim_one)
Date: 2003-02-28 21:45
Message:
Logged In: YES
user_id=31435
Short course: returning NotImplemented (== falling back to
address comparison, unless the other comparand wants to
handle it) instead for __eq__ and __ne__ would be fine.
That would be similar to what was done for datetime
objects in 2.3a2. The other 4 relationals shouldn't be
changed.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-02-28 21:08
Message:
Logged In: YES
user_id=80475
The OP added this note at the behest of
comp.lang.python. The thread is at:
http://tinyurl.com/6n91 and includes
some comments by Tim.
Assigning to Guido to decide whether he wants to keep
that behavior.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=693121&group_id=5470