How do I do this? (eval() on the left hand side)
Caleb Hattingh
caleb1 at telkomsa.net
Tue Dec 7 23:28:39 EST 2004
Sure, ok, I think I am with you now.
You get a (e.g.) variable name as a string, and you KNOW how to evaluate
it with "eval", but you also want to be able to assign back to (through)
the string representation?
One way (if I understand you correctly) is with the globals or locals
dicts. Try this in IDLE:
'>>> a = 3
'>>> y = 'a'
'>>> eval(y)
3
'>>> d = locals() # Get a dictionary of local variables
'>>> d['a']
3
'>>> d[y]
3
'>>> d[y] = 8 # y is a string = 'a'
'>>> a # The value of a is changed.
8
'>>>
Is this kinda what you mean? I'm still new at this (and don't know REXX
from Adam).
Thanks
Caleb
>
> There are many situations where this is useful. For instance, you
> might be
> 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
> instance).
More information about the Python-list
mailing list