Unbinding multiple variables

Nick Coghlan ncoghlan at iinet.net.au
Sun Jan 23 01:38:16 EST 2005


Bengt Richter wrote:
> (OTOH, deletions of actual local bindings do seem to propagate back into a previously
> bound value of locals on exit, and a new call to locals() seems to return the same identical
> object as before, so I'm not sure I believe the <type 'dict'>, unless it has a special slot
> and it is automatically updated at exit. But a local bare name assignment or deletion doesn't
> immediately propagate. But it does on exit. So the <type 'dict'> returned by locals() has
> a special relationship to the function it reflects, if it is otherwise a normal dict:

CPython frames keep two copies of their locals around - the "fast" locals, which 
are actually used by the code (and stored in an array for fast access, hence the 
name), and a Python-readable copy in a dictionary (this is the dictionary 
returned by locals()).

Various things trigger updates from the fast locals to the locals dictionary. 
Updates in the other direction are far less common (exec without an 'in' clause, 
star imports, and monkeying with the frame via the C API are the only cases I am 
aware of).

Cheers,
Nick.



-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list