By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Shortcut to initialize variables

P: n/a
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. I don't think
this works, but I'm wondering if I can do something similar to this:

from string import ascii_lowercase

class Blah:
def __init__(self):
for letter in ascii_lowercase:
setattr(self,letter,0)

Is there a way I can do something like this without using classes?

Thanks

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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. I don't think
this works, but I'm wondering if I can do something similar to this:

from string import ascii_lowercase

class Blah:
def __init__(self):
for letter in ascii_lowercase:
setattr(self,letter,0)

Is there a way I can do something like this without using classes?


This is a very common request on this list. The usual advice is to use a dictionary rather than defining variables, e.g.
letters = {}
for letter in ascii_lowercase:
letters[letter] = 0
But I'm becoming curious about why this is so commonly requested. What is your background that you (and others) would think of this as a solution? Is there a language where it is easy to do this sort of thing, and where the resulting variables are easily used?

Thanks,
Kent
Jul 19 '05 #2

P: n/a
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_lowerc ase, 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
Jul 19 '05 #3

P: n/a
Oops, I probably should have tried searching the list first. My
background is strictly academic. I was switching languages so often I
never got really familiar with any of them. Maybe C for a while, but
I've forgotten alot. I'm hoping python will be the last language I ever
need. :) I don't know why I didn't turn to dictionaries first. It does
seem to be the obvious solution.

I'm writing a program that will take substitution and transposition
cipher texts and spit out plain text with no human input. So I suppose
I'll have dictionaries of digraphs and trigraphs too; for frequency
analysis. Do you think this is to heavy of a project to learn the
language?

Thanks for the quick feedback

Jul 19 '05 #4

P: n/a
Andrew wrote:
I'm writing a program that will take substitution and transposition
cipher texts and spit out plain text with no human input. So I suppose
I'll have dictionaries of digraphs and trigraphs too; for frequency
analysis. Do you think this is to heavy of a project to learn the language?


Not at all, provided you're somewhat expert in the domain already, and
are just using it as a means to help learn Python.

If you are learning both Python and how to break ciphers at the same
time, I'd personally call it a "little" heavy... ;-)

-Peter
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.