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

"Martin v. Löwis" martin at v.loewis.de
Mon Jun 14 00:18:18 EDT 2004


Oliver Schoenborn wrote:
>>I'm still uncertain, though, as to what objects precisely should get
>>the cleanup calls: *All* objects that have registered with the cleaner,
>>or only some of them?
> 
> 
> I don't see why it wouldn't be all objects. You'll have to show me an
> example for when that might be the case. Each object that has registered is
> saying, "I need cleanup at exit of scope, because I should be the only one
> left". If you don't clean it up you are not upholding your side of the
> contract.

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.

Then, g returns, and f tries to use the objects stored in a.
However, that object already has been finally-ized, so the object
is no longer functional.

I doubt this is the intended semantics, but I don't know what
the intended semantics is instead.

Regards,
Martin




More information about the Python-Dev mailing list