From dictionary keys to variables

Jeff Shannon jeff at ccvcorp.com
Tue Oct 12 14:47:34 EDT 2004


Michael Foord wrote:

>Jeff Shannon <jeff at ccvcorp.com> wrote in message news:<10mme8ars7bdaeb at corp.supernews.com>...
>  
>
>>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.)
>>
>>    
>>
>
>It's not really optimisation. There are two factors, I write lot's of
>little scripts that pull there paths/config options out of a config
>file in this way. With something like the above or 'globals()[entry]'
>I can just cut and paste a chunk of code. That means I can add/change
>the variable names (which correspond to keywords in the config file)
>by just changing the name once in the 'valuelist'. I can then access
>the variable directly without having to keep *typing* the
>config['..... bit.. The only optimisation is on my wrist !!
>  
>

Oof.  Don't cut and paste code if you can avoid it.  Instead, make 
utility modules and import them.  Much *much* safer.  Every time you cut 
and paste, you run the risk of forgetting to rename something, or 
copying a name that isn't defined in your new script, or any number of 
other minor little glitches.  (I do much of my workday programming in an 
environment that doesn't support modularization well at all, and we have 
no real options for code reuse other than to either cut and paste, or 
retype from scratch.  I know first-hand just how easy it is to make 
mistakes when cutting and pasting, because I do it all the freakin' 
time....  bleah!  I *so* wish I could use Python for all of that...)

And really, if you're trading readability for typing ease, you're 
getting a bad deal.  You're very likely to read that code many more 
times than you're going to type it, so a slight addition to the 
difficulty of reading and understanding it will soon make up for a bit 
of extra typing.  More importantly, if there's any chance that anyone 
*else* will ever look at your code, it's much nicer for *them* to not 
have to figure out how the heck this variable got here...   And even if 
your code is something that you expect to be a solitary project that'll 
never be distributed to anyone else, it's still a good idea to get in 
the habit of writing readable code.  If nothing else, you never know 
when you might want to post segments of it to c.l.py for 
assistance/advice. ;)

(All of this being strictly my personal opinion, of course, which is 
probably worth about as much as you paid for it....)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list