How to turn a variable name into a string?

André Roberge andre.roberge at gmail.com
Fri Mar 11 17:16:04 EST 2005


stewart.midwinter at gmail.com wrote:
> lots of good answers there, and quickly, too!
> 
> I can see that I need to explain a bit further what I'm up to.
> 
> I have a number of variables (environmental variables, actually), most
> of which will have a value. But some may not have been found by
> os.environ.get(), so I set those to None. Now, if any of them are None,
> the app cannot proceed, so I want to test for this and warn the user.
> I could do something like this (actually, there are more than 3 vars):
> a = "c:\\programs"
> b = "d:\\data"
> c = None  (result of an assignment after the os.environ.get() returned
> a KeyError).
> if (a is None) or (b is None) or (c is None):
>     #do something here
>     print 'you are missing some env vars'
> 
> But, that seemed clumsy to me, so I wanted to do something more
> pythonic, hence my previous post.   So, any suggestions?
> 
Why not build a list of problematic variables as you assign them?
In this case, you would have
problem_list = [c]

if problem_list != []:
     # do something here

etc.

André




More information about the Python-list mailing list