"Extracting" a dictionary

Peter Hansen peter at engcorp.com
Mon May 17 16:54:17 EDT 2004


Leif K-Brooks wrote:

> Daniel Klein wrote:
> 
>> Is there a way to 'extract' a dictionary into the current namespace? 
>> That is, if you have
>> {'foo' : 23, 'bar' : 42}
>> you would get a variable foo with value 23 and a variable bar with 
>> value 42? Such a function would of course only work on string keys and 
>> would probably have to check that, but still, it sounds practical 
>> enough that surely someone else thought of it before.
> 
> 
>  >>> vars = {'foo': 23, 'bar': 42}
>  >>> locals().update(vars)
>  >>> foo
> 23
>  >>> bar
> 42

Except note from this page 
http://docs.python.org/lib/built-in-funcs.html#built-in-funcs that
"""
locals()

   Update and return a dictionary representing the current local symbol
   table. Warning: The contents of this dictionary should not be
   modified; changes may not affect the values of local variables used by
   the interpreter.
"""

So Don't Do That.

-Peter



More information about the Python-list mailing list