[ python-Bugs-1721812 ] A subclass of set doesn't always have __init__ called.
SourceForge.net
noreply at sourceforge.net
Sat May 19 18:40:02 CEST 2007
Bugs item #1721812, was opened at 2007-05-19 09:03
Message generated for change (Comment added) made by rhettinger
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1721812&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
>Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: David Benbennick (dbenbenn)
Assigned to: Raymond Hettinger (rhettinger)
Summary: A subclass of set doesn't always have __init__ called.
Initial Comment:
Consider the following code:
>>> class foo(set):
... def __init__(self, iter):
... print "foo.__init__"
... set.__init__(self, iter)
...
>>> x = foo([1,2])
foo.__init__
>>> y = foo([2,3])
foo.__init__
>>> x & y
foo([2])
As you can see, the foo.__and__ method creates a new foo object without calling foo.__init__.
The problem is in Objects/setobject.c:make_new_set().
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-05-19 11:40
Message:
Logged In: YES
user_id=80475
Originator: NO
Hmm, that's a bummer.
It is probably going to have to change.
Will look it and fix it when I get a chance.
----------------------------------------------------------------------
Comment By: Georg Brandl (gbrandl)
Date: 2007-05-19 11:32
Message:
Logged In: YES
user_id=849994
Originator: NO
There is a difference: in the list and int case, the new object is of the
base type.
In the set case, the new object is of type foo.
But the constructor signature argument is a valid one.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2007-05-19 11:25
Message:
Logged In: YES
user_id=80475
Originator: NO
Sorry, this is just the way Python works. Given that the __and__
operation has no way of knowing the signature of your subclass contructor,
it will produce a new set from your two foo inputs. This design prevades
the language (for example list and int have similar behaviors). Recommend
closing this as not-a-bug:
>>> class foo(list):
def __init__(self, iter):
print 'foo.__init__'
list.__init__(self, iter)
>>> x = foo([1,2])
foo.__init__
>>> y = foo([2,3])
foo.__init__
>>> x + y
[1, 2, 2, 3]
>>> type(_)
<type 'list'>
>>>
>>> class foo(int):
pass
>>> foo(1) + foo(2)
3
>>> type(_)
<type 'int'>
----------------------------------------------------------------------
Comment By: Georg Brandl (gbrandl)
Date: 2007-05-19 11:11
Message:
Logged In: YES
user_id=849994
Originator: NO
Wasn't that fixed some time ago?
@OP: what Python version do you use?
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1721812&group_id=5470
More information about the Python-bugs-list
mailing list