"Extracting" a dictionary

Jason Mobarak jason at __no_spam__mobarak.name
Tue May 18 00:36:19 EDT 2004


Daniel Klein wrote:
> Hello,
> 
> I'm quite new to Python, and since a not-so-superficial look into the 
> docs didn't answer my question (although it still feels quite basic), I 
> decided to turn to this place:
> 
> 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.
> 
> Daniel
> 

Is this illegal?

 >>> import __main__
 >>> d = {'foo':'oof','bar':'rab','baz':'zab'}
 >>> for k,v in d.items():
...   setattr(__main__, k, v)
...
 >>> foo,bar,baz
('oof', 'rab', 'zab')

--
Jason



More information about the Python-list mailing list