[patch] tiny documentation fix
Robey Pointer
robey at lag.net
Wed Nov 2 23:17:22 GMT 2005
On 2 Nov 2005, at 15:01, Martin Pool wrote:
> On 02/11/05, Robey Pointer <robey at lag.net> wrote:
>
>> """
>> Python destructors (``__del__``) work differently from those in other
>> languages. In particular, bear in mind that destructors may be
>> called immediately after an object becomes dereferenced, or may not
>> be called at all (if the object is in a cycle).
>> """
>>
>
> OK, I changed it. Note however that Python can collect objects in a
> cycle; things failing to get collected at all probably requires more
> complex situations.
The rule seems to be: if one or more of the objects in a cycle has a
__del__ method, none of the cycle will be collected. There was a
long thread on this on py-dev earlier in the summer. People way
smarter than me assured us that it's not an easy problem to solve.
I went through a few days of sweat on paramiko to ensure that the
important objects aren't part of cycles, and have __del__ methods
that are equivalent to close(), because python programmers have come
to expect that objects with resources get cleaned up as soon as they
pass out of scope.
robey
PS. Here's a demo which I find easier to understand than any
explanation:
>>> class C (object):
... def __init__(self, name): self.name = name
... def __repr__(self): return '<object %r>' % self.name
... def __del__(self): print 'DELETE %r' % self
...
>>> def function_scope():
... x = C('function scope')
...
>>> function_scope()
DELETE <object 'function scope'>
>>> g = C('global scope')
>>> cycle1 = C('cycle 1')
>>> cycle2 = C('cycle 2')
>>> cycle1.other = cycle2
>>> cycle2.other = cycle1
>>> ^D
DELETE <object 'global scope'>
$
More information about the bazaar
mailing list