Shortcut to initialize variables

Steven Bethard steven.bethard at gmail.com
Fri Jun 17 14:57:37 EDT 2005


Andrew wrote:
> Newb here... For one of my programs I want to initialize a variable for
> each letter of the alphabet. For example, a,b,c = 0,0,0.

Why do you want to do this? This looks like a particularly bad idea to 
me.  Can't you just use a dict of the "variables", e.g.:

py> d = dict.fromkeys(string.ascii_lowercase, 0)
py> d['a']
0
py> d['x']
0
py> d['q']
0

If you insist on updating the module globals, you can do something like:

py> globals().update(dict.fromkeys(string.ascii_lowercase, 0))
py> a
0
py> x
0
py> q
0

but I find that just about every use of globals() has a bad code smell.

STeVe



More information about the Python-list mailing list