Shortcut to initialize variables

Discussion in 'Python' started by Andrew, Jun 17, 2005.

  1. Andrew

    Andrew Guest

    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
     
    Andrew, Jun 17, 2005
    #1
    1. Advertising

  2. Andrew

    Kent Johnson Guest

    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
     
    Kent Johnson, Jun 17, 2005
    #2
    1. Advertising

  3. 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
     
    Steven Bethard, Jun 17, 2005
    #3
  4. Andrew

    Andrew Guest

    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
     
    Andrew, Jun 17, 2005
    #4
  5. Andrew

    Peter Hansen Guest

    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
     
    Peter Hansen, Jun 18, 2005
    #5
  6. Kent Johnson wrote:
    > letters = {}
    > for letter in ascii_lowercase:
    > letters[letter] = 0


    Or more simply:

    letters = dict.fromkeys(ascii_lowercase, 0)
     
    Leif K-Brooks, Jun 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Madden
    Replies:
    4
    Views:
    756
    Timothy Madden
    Sep 14, 2004
  2. David T. Ashley
    Replies:
    6
    Views:
    553
    Tim Rentsch
    Dec 5, 2004
  3. Pekka Niiranen
    Replies:
    1
    Views:
    333
    Alex Martelli
    Nov 6, 2004
  4. Replies:
    1
    Views:
    361
    Chris Brat
    Sep 17, 2006
  5. Jean-Christophe

    Q: Local variables initialization shortcut.

    Jean-Christophe, Jun 2, 2012, in forum: C Programming
    Replies:
    48
    Views:
    912
Loading...

Share This Page