[Tutor] Re: creating variables from the 'key' of a dictionary.

Andrei project5 at redrival.net
Fri Jul 2 15:42:13 EDT 2004


Larry Blair wrote on Fri, 2 Jul 2004 10:16:54 -0700:

<snip>
> My question is there a way to create a variable with the name of the key in
> a dictionary i.e.
> 
> para = {}
> para['source_server'] = 'cm05'
> 
> at this point I want to create a variable with the name source_server and a
> value of 'cm05'.  

Use the globals() dictionary (it contains a dictionary mapping all variable
names to corresponding objects):

>>> globals()['source_server'] = 'cm05'
>>> source_server
'cm05'

But I think Jeff's suggestions for a settings module shared by all your
apps is a better idea.

-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.




More information about the Tutor mailing list