How do I do this? (eval() on the left hand side)
russblau at hotmail.com
Tue Dec 7 16:23:01 EST 2004
"It's me" <itsme at yahoo.com> wrote in message
news:Y0ptd.39767$6q2.23845 at newssvr14.news.prodigy.com...
> In REXX, for instance, one can do a:
> interpret y' = 4'
> Since y contains a, then the above statement amongs to:
> a = 4
> There are many situations where this is useful. For instance, you might
> getting an input which is a string representing the name of a variable and
> you wish to evaluate the expression (like a calculator application, for
In Python, the canonical advice for this situation is, "Use a dictionary."
This has a number of advantages, including keeping your user's namespace
separate from your application's namespace. Plus it's easier to debug and
maintain the code.
But, if you absolutely, positively have to refer to your variable
indirectly, you could do:
exec "%s = 4" % y
If y refers to the string "a", this will cause the variable a to refer to
the value 4.
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
More information about the Python-list