[Python-Dev] Object finalization for local (ie function) scopes

"Martin v. Löwis" martin at v.loewis.de
Thu Jun 10 16:14:25 EDT 2004

Oliver Schoenborn wrote:
>>Python does provide finalization, and in a reliable way: the finalizer
>>is called before the object goes away, guaranteed.
> The python docs are a bit confusing to me in this regard, so allow me to
> quote a few section:
>   From Python 2.3 docs:
>       "An implementation is allowed to postpone garbage collection
>       or omit it altogether -- it is a matter of implementation
>       quality how garbage collection is implemented, as long as no
>       objects are collected that are still reachable.

This is precisely what I said: the object is finalized before it
goes away. If it does not go away, it might not get finalized.

> So it seems that having a __del__ is ok *most* of the time, and that if you
> find a mem leak, verify if you have cyclical refs, and modify algorithm to
> break cycle because gc won't be able to call __del__; however if an
> exception gets thrown, you can't rely on __del__ getting called. Since
> exceptions are so common, I have to conclude that you can never rely on
> __del__ to free resources. Where am I wrong in this reasoning?

Yes, you are wrong. The Python language specification does not make
guarantees. However, specific implementations do make specific
guarantees beyond what the language specification guarantees.

Your proposed mechanism cannot be added to the Python language
specification because there might be Python implementations for
which the mechanism is difficult to implement.


More information about the Python-Dev mailing list