How to turn a variable name into a string?

Lonnie Princehouse finite.automaton at gmail.com
Sat Mar 12 00:11:03 CET 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