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

Oliver Schoenborn oliver.schoenborn at utoronto.ca
Mon Jun 14 11:08:10 EDT 2004

From: "Martin v. Löwis" <martin at v.loewis.de>
> Consider this example, where X is a class that needs finally-ization,
> and f and g are functions that participate in the protocol:
> def f():
>    a = X()
>    g()
>    a.do_something()
> def g():
>    b = X()
> Now, when f starts, an X instance is registered with the cleaner,
> and this is assigned to the local variable a. Then g is invoked.
> This creates another X instance that is also registered with the
> cleaner. Then g returns, and all objects registered with the
> cleaner are finally-ized.
> At the moment, there are two objects
> registered with the cleaner, so they are both finally-ized.

Oh no, absolutely not! The objects are finaly-izated only for the frame
where created. This is ensured because the cleaner uses a stack (unrelated
to the frame stack), one item in the stack per function call. You will see
this clearly if you look at the latest detscope.py. b is cleaned up upon
return from g, and a will be cleaned up only upon return from f.


More information about the Python-Dev mailing list