From dictionary keys to variables

Discussion in 'Python' started by Michael Foord, Oct 11, 2004.

  1. I use a module called COnfigObj to read my config files into python.
    It exposes them as a dictionary.

    I often find myself writing code like :

    valuelist = ['name1', 'name2', 'name3'....]
    config = ConfigObj(filename, configspec=valuelist)
    name1 = config['name1']
    name2 = config['name2']
    name3 = config['name3']

    Now is there any reason not to 'cheat' and use __main__.__dict__ to
    create the variables ?

    valuelist = ['name1', 'name2', 'name3'....]
    config = ConfigObj(filename, configspec=valuelist)
    import __main__
    for entry in valuelist:
    __main__.__dict__[entry] = config[entry]

    (The configspec is optional - but helps with ConfigObj).


    Michael Foord, Oct 11, 2004
    1. Advertisements

  2. Michael Foord wrote:
    Why cheat? There is legal way to do that:

    for entry in valuelist:
    globals()[entry] = config[entry]
    Timothy Babytch, Oct 11, 2004
    1. Advertisements

  3. Michael Foord

    Jeff Shannon Guest

    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.)

    Jeff Shannon
    Credit International
    Jeff Shannon, Oct 12, 2004
  4. Ahh... I didn't realise you could do assignment into globals().........


    Michael Foord, Oct 12, 2004
  5. 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 !!

    Anyway, thanks. You may still be right of course...


    Michael Foord, Oct 12, 2004
  6. Michael Foord

    Jeff Shannon Guest

    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 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
    Credit International
    Jeff Shannon, Oct 12, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.