How to turn a variable name into a string?

Lonnie Princehouse finite.automaton at gmail.com
Fri Mar 11 18:11:03 EST 2005


Perhaps try something like this?

variables = ['a', 'b', 'c']

defaults = {
  'a': 'c:\\programs',
  'b': 'd:\\data',
  # etc
}

try:
  settings = dict([(v, os.environ.get(v, defaults[v])) for v in
variables])
except KeyError, k:
  # handle missing variable
  print "you have a problem with %s" % k.args[0]

# Now if you really must have these as independent variables
globals().update(settings)




More information about the Python-list mailing list