One Python 2.1 idea

Alex Martelli aleaxit at yahoo.com
Tue Dec 26 19:16:12 EST 2000


"Darrell Gallion" <darrell at dorb.com> wrote in message
news:mailman.977868490.18316.python-list at python.org...
    [snip]
> You lost me here Tim.
>
> >>> class A:
> ...     def f(self):
> ...             pass
> ...
> >>> a=A()
> >>> a.f.im_self is a
> 1

In this case, a.f (the bound-method) is transiently
constructed from A.f (the unbound-method), and
goes away if no reference to it is kept; and indeed
after this sequence:

>>> sys.getrefcount(a)
2

so variable 'a' is the only reference extant to this
instance object (remember getrefcount always
returns one more!-).

> >>> a.f=a.f
> >>> a.f.im_self is a
> 1
> >>>
>
> Don't see what's changed after a.f=a.f

Now, you've bound a reference to the bound-method,
so, it's kept alive -- and holds a reference to a.  Indeed,
now:

>>> sys.getrefcount(a)
3

This would be true wherever you kept a reference to
bound-method a.f -- but if that reference was in some
slot not connected with a, no cycles would exist.  As
the slot you're using (a.f) is in a's instance dictionary,
then the references form a circular 'loop':

a -> its instance dictionary -> boundmethod a.f -> ... -> a


Alex







More information about the Python-list mailing list