From dictionary keys to variables
jeff at ccvcorp.com
Tue Oct 12 03:55:52 CEST 2004
Michael Foord wrote:
>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']
Any reason why it's necessary to define variables instead of just using
the dict? You're probably not really saving anything by doing this...
there *might* be a slight savings in typing if you use the names
numerous times, but directly accessing the dictionary makes it clearer
where your values are coming from. And dict lookups are fast, so unless
you're using these valuables repeatedly inside a tight loop, you won't
get significant speed savings. (If you *are* using some inside a tight
loop, then it's sensible to hoist the lookup out of the loop, but don't
do this until you know that that loop is a significant source of
slowdown -- as they say, premature optimization is the root of all evil.)
More information about the Python-list