One Python 2.1 idea

Darrell Gallion darrell at dorb.com
Tue Dec 26 17:14:50 EST 2000


From: "Tim Peters"
> [Thomas Wouters]
> > ...
> > Actually, it's even worse :) Methods are never stored in an
> > instance's dict, but are created 'on the fly' from the class's
> > unbound method. The binding of the method isn't too expensive,
> > but it isn't totally free either (if anything, it's a dict 'miss'
> > and an aditional dict lookup in the class's dict. More than one
> > if you use deep or multiple inheritance (or both.))
> >
> > You *can* speed it up, by doing 'x.foo = x.foo' somewhere early
> > in your call chain :) ...
>
> Note:
>
> >>> class Oops:
> def foo(self):
> pass
>
> >>> x = Oops()
> >>> x.foo = x.foo
> >>> x.foo.im_self is x
> 1
> >>>
>
> Oops!  That is, x is now involved in a reference cycle (x's dict contains
a
> bound method that points back to x).  Before 2.0, that's a memory leak.
In
> 2.0, it's a leak only if the class (unlike Oops above) has a __del__
method
> (or you've disabled cyclic gc).
>

You lost me here Tim.

>>> class A:
...     def f(self):
...             pass
...
>>> a=A()
>>> a.f.im_self is a
1
>>> a.f=a.f
>>> a.f.im_self is a
1
>>>

Don't see what's changed after a.f=a.f

--Darrell








More information about the Python-list mailing list