Strange behavior of weak references (weakref module)

Martin von Loewis loewis at informatik.hu-berlin.de
Fri Dec 21 11:52:53 EST 2001


tiberius at ms28.hinet.net (Tiberius Teng) writes:

> I found this behavior (bug?) when I want to create weak reference to a
> instance method. It's hard to describe (using my poor English writing
> ability), so I just attach the way to reproduce the situation. And
> notice that the id() of a instance method reference changes after each
> function call!

I can't see anything strange here. A.test returns a different object
every time evaluated; this has nothing to do with weak references.
The object that A.test returns is an unbound method. 

That method is only created when somebody accesses it; the class
itself does not keep a reference to the method object (it only keeps a
reference to the function object inside the method object). If the
class would keep a reference of the method object, you'd get a
circular reference.

Regards,
Martin




More information about the Python-list mailing list