weakrefs and bound methods

Steven Bethard steven.bethard at gmail.com
Sun Oct 7 13:25:48 EDT 2007


Mathias Panzenboeck wrote:
> Marc 'BlackJack' Rintsch wrote:
>> ``del b`` just deletes the name `b`.  It does not delete the object. 
>> There's still the name `_` bound to it in the interactive interpreter. 
>> `_` stays bound to the last non-`None` result in the interpreter.
> 
> Actually I have the opposite problem. The reference (to the bound method)
> gets lost but it shouldn't!

Ahh, so you expected that ``Wrapper(self._foo)`` would not immediately 
lose the reference?  It will, because every time you write 
``self._foo``, a new bound method is created::

     >>> class C(object):
     ...     def foo(self):
     ...         pass
     ...
     >>> f = C.foo
     >>> g = C.foo
     >>> id(f), id(g)
     (14931448, 14891008)

Thus, there is only the one reference to the bound method, and by 
wrapping it in a weakref, you are allowing it to disappear immediately::

     >>> x = weakref.ref(C.foo)
     >>> print x()
     None

What behavior do you want here?  That is, when were you hoping that the 
bound method would disappear?

STeVe



More information about the Python-list mailing list