problem with weakref.proxy
__peter__ at web.de
Sat Jan 31 18:58:36 CET 2004
Peter Otten wrote:
> I'd recommend being lazy and using a WeakValueDictionary instead of
> building the machinery on your own. Your code would then look similar to
If you want to keep your original design, just use ref instead of proxy:
print "all GObs, sorted by priority:"
for i in GOb.__olist:
print "weakref.proxy callback"
def __init__(self, name="GOB", priority=0):
GOb.__olist.sort(lambda x, y: cmp(x(), y()))
print "Destruktor called for GOB " + self.name
print self.name + " " + str(self.priority)
def __cmp__(self, other):
if self.priority < other.priority:
elif self.priority == other.priority:
if __name__ == '__main__':
print "delete T1:"
The line that failed in your original version is
because x is found in __olist by comparing against the list items - and the
proxy automatically delegates this to the no longer existing object. With
ref you have to explicitly dereference by calling (as seen in
__olist.sort() in the __init__() method).
As that step is not performed by the list.remove() method, only ref instead
of GOb instances are compared and the callback succeeds.
More information about the Python-list