One Python 2.1 idea

Tim Peters tim.one at home.com
Tue Dec 26 13:16:49 EST 2000


[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).

gotta-love-the-way-"x.foo=x.foo"-looks-though<wink>-ly y'rs  - tim





More information about the Python-list mailing list