Using dictionaries

Wolfgang Grafen wolfgang.grafen at marconi.com
Tue Dec 5 05:15:55 EST 2000


sorular wrote:
> 
> G'day
> 
> I am trying to write a python program where a "configuration.py" will
> have all the user entered values in a dictionary format and a
> "main.py" which will import these and use it as local parameters.
> 
> But, there are quite a few values in the dictionary
> ie
> dict = { 'key1':'value1' , ' key2':'value2' , ......'keyN':'value2'}
> 
> and in the "main.py" these will be all renamed with the local names
> ie
> Val1 = dictionary.dict['key1']
> Val2 = dictionary.dict['key2']
> ....
> ValN = dictionary.dict['keyN']
>         etc....
> 
> But overall, I still have a large numbe of values (ValN) to list... !
> 
> Q1- Is there a more efficient way to do this ? I thought about using a
> for loop but that didnt work....
> 
> Q2 - would it be possible only to list the parameters in first file
> (ie dictionary.py
>         key1 = value1
>         key2 = value2
>         .....
>         keyN = valueN
> )
> and inside main.py, do a simple import...
>         import dictionary *
> so that key1 can be used inside main.py as a local parameter?
> 
> thank
> 
> HK
What about this?

>>> locals().update(dictionary)

or

>>> globals().update(dictionary)

>>> dictionary={"b":3}
>>> globals().update(dictionary)
>>> b
3



More information about the Python-list mailing list