[ python-Bugs-1317376 ] irregular behavior within class using __setitem__
SourceForge.net
noreply at sourceforge.net
Sat Oct 8 10:34:50 CEST 2005
Bugs item #1317376, was opened at 2005-10-08 17:27
Message generated for change (Comment added) made by ncoghlan
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1317376&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 Interpreter Core
Group: Python 2.3
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: capnSTABN (capnstabn)
Assigned to: Nobody/Anonymous (nobody)
Summary: irregular behavior within class using __setitem__
Initial Comment:
i found two bugs vaguely similar to this posted but i
think they were different bugs in theory, and both were
deemed 'features' anyway so ... :p
basically the class is a modified list, being used as a
queue
what happens is, when hotswapping the __setitem__
functionality, unless explicitly calling __setitem__, the
old __setitem__ continues to be called
this is very odd and as far as i can tell the behavior is
undocumented
class Queue(list):
def __setitem__(self, a, v):
print "old setitem"
def mysetitem(self, i, v):
print "new setitem"
def use_mysetitem(self):
self.__setitem__ = self.mysetitem
x = Queue()
x[1] = 2
x.__setitem__(2, 3)
x.use_mysetitem()
x[3] = 4
x.__setitem__(4, 5)
----------------------------------------------------------------------
>Comment By: Nick Coghlan (ncoghlan)
Date: 2005-10-08 18:34
Message:
Logged In: YES
user_id=1038590
And now that I've read the bug report properly, I'm closing
it as "Not a bug" :)
To affect the operators you need to alter the slot on the
*class*, not on the instance, but your "use_mysetitem"
method only affects the current instance - it shadows the
slot in the instance dictionary, but the interpreter is only
interested in the slots in the class itself.
To make the method work, it needs to be a class method:
def use_mysetitem(cls):
cls.__setitem__ = cls.mysetitem
use_mysetitem = classmethod(use_mysetitem)
Of course, it will then affect *all* instances of the class
once the change is made.
You can get per-instance swapping by writing the slot method
in such a way that it delegates to a normal method, which
you can then alter on a per-instance basis. The wisdom of
actually *doing* that is debatable though ;)
----------------------------------------------------------------------
Comment By: Nick Coghlan (ncoghlan)
Date: 2005-10-08 18:22
Message:
Logged In: YES
user_id=1038590
Ah, sorry. I was looking at the wrong part of the printout.
----------------------------------------------------------------------
Comment By: capnSTABN (capnstabn)
Date: 2005-10-08 18:14
Message:
Logged In: YES
user_id=1126596
err, yeh it does 8)
the last TWO prints should display new setitem
----------------------------------------------------------------------
Comment By: Nick Coghlan (ncoghlan)
Date: 2005-10-08 18:09
Message:
Logged In: YES
user_id=1038590
This problem doesn't exist in Python 2.4:
Py> class Queue(list):
... def __setitem__(self, a, v):
... print "old setitem"
... def mysetitem(self, i, v):
... print "new setitem"
... def use_mysetitem(self):
... self.__setitem__ = self.mysetitem
...
Py> x = Queue()
Py>
Py> x[1] = 2
old setitem
Py>
Py> x.__setitem__(2, 3)
old setitem
Py>
Py> x.use_mysetitem()
Py> x[3] = 4
old setitem
Py>
Py> x.__setitem__(4, 5)
new setitem
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1317376&group_id=5470
More information about the Python-bugs-list
mailing list