Shortcut to initialize variables

A

Andrew

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
 
K

Kent Johnson

Andrew said:
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
 
S

Steven Bethard

Andrew said:
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
 
A

Andrew

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
 
P

Peter Hansen

Andrew said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top