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

Josiah Carlson jcarlson at uci.edu
Thu Jan 18 03:38:41 CET 2007


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?

If you are talking about the locals in the scope of a function, it is an
artifact of how the locals array is created.  That is to say, the locals
of a function are a flat array, and are decref'd in-order.  That
ordering is the original assignment ordering in the function, which is
an artifact of how the compiler goes from local name -> FAST_LOCALS
index.  I don't believe it is an "important feature", but to decref in
any other order would be silly and/or unintuitive.


 - Josiah



More information about the Python-Dev mailing list