From dictionary keys to variables

Michael Foord fuzzyman at gmail.com
Mon Oct 11 08:33:25 EDT 2004


I use a module called COnfigObj to read my config files into python.
It exposes them as a dictionary.

I often find myself writing code like :

valuelist = ['name1', 'name2', 'name3'....]
config = ConfigObj(filename, configspec=valuelist)
name1 = config['name1']
name2 = config['name2']
name3 = config['name3']
.
.

Now is there any reason not to 'cheat' and use __main__.__dict__ to
create the variables ?

valuelist = ['name1', 'name2', 'name3'....]
config = ConfigObj(filename, configspec=valuelist)
import __main__
for entry in valuelist:
        __main__.__dict__[entry] = config[entry]

(The configspec is optional - but helps with ConfigObj).

Regards,


Fuzzy

http://www.voidspace.org.uk/at;antibots/pythonutils.html



More information about the Python-list mailing list