[Python-Dev] Deletion order when leaving a scope?

Thomas Wouters thomas at python.org
Thu Jan 18 10:55:33 CET 2007


On 1/18/07, Larry Hastings <larry at hastings.org> wrote:
>
>
>
> I just ran a quickie experiment and determined: when leaving a scope,
> variables are deleted FIFO, aka in the same order they were created.  This
> surprised me; I'd expected them to be deleted LIFO, aka last first.  Why is
> it thus?  Is this behavior an important feature or an irrelevant
> side-effect?
>

Please regard it as an irrelevant side-effect. If you want objects to be
cleaned up in a particular order, you should enforce it by having one of
them refer to the other. A great many details can affect the order in which
variables are cleaned up, and that only decreases refcounts of the actual
objects -- a great many other details can then affect the order in which any
objects left with a 0 refcount are actually cleaned up. Even not counting
the more complicated stuff like GC and funky __del__ methods, just having
'import *' or a bare 'exec' in your function can change the order of
DECREFs.

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070118/56e22b46/attachment.html 


More information about the Python-Dev mailing list