implicit variable declaration and access

Ali Razavi arazavi at swen.uwaterloo.ca
Mon Jun 13 12:34:39 EDT 2005


Ali Razavi wrote:
> Is there any reflective facility in python
> that I can use to define a variable with a
> name stored in another variable ?
> like I have :
> x = "myVarName"
> 
> what can I do to declare a new variable with the name of the string
> stored in x. And how can I access that implicitly later ?
Got it! use higher order functions like Lisp!

code = x + '= 0'
exec(code)

code = 'print ' + x
exec(code)






More information about the Python-list mailing list