Strange behavior of weak references (weakref module)

Tiberius Teng tiberius at ms28.hinet.net
Tue Dec 18 09:01:10 EST 2001


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!

ActivePython 2.1.1, build 212 (ActiveState)
Python 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
>>> from weakref import *
>>> class A:
...     def test(self):
...             return 3+4
...
>>> ref(A.test)
<weakref at 925704; dead>
>>> a=A.test
>>> ref(a)
<weakref at 0x3a9184; to 'instance method' at 0x3ae624>
>>> a=A()
>>> ref(a.test)
<weakref at 925704; dead>
>>> b=a.test
>>> ref(b)
<weakref at 0x3a9184; to 'instance method' at 0x3ae624>
>>> id(b)
9549988
>>> a.test()
7
>>> b=a.test
>>> id(b)
3860004



More information about the Python-list mailing list