Create a variable "on the fly"

bruno modulix onurb at xiludom.gro
Wed Jul 27 13:25:35 EDT 2005


Paul D.Smith wrote:
> Can Python create a variable "on-the-fly".  For example I would like
> something like...
> 
> make_variable('OSCAR', 'the grouch');
> print OSCAR;
> 
> ...to output...
> 
> the grouch
> 
> Anything like this in Python?

The bad news is that yes, there is something "like this" in Python.

The good news is that I won't tell you more about it, since it's a very
bad practice(tm). The good practice is to put your vars in a dict.

> And in case anyone is interested, I want to instantiate a set of variables
> based on environment variables without using os.environ everywhere

 env = os.environ

Now you just have to look at env['MY_ENV_VAR'] !-)

> by having
> a look instantiate Python variables of the appropriate type.

What do you mean "of the appropriate type" ? You want to typecast (eg.
from string to numeric) ? Then you need to know what env var must be
casted to what type ? Then you don't need to create variables 'on the fly' ?

I must have missed something...


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list