String as a Variable?

Jonathan Polley
Sat Feb 3 21:34:41 CET 2001

I actually use the exec() and eval() functions.  If I need to assign to
the string/variable, I will use exec():

My_Variable = "This_Thing"
My_Expression = "= 15.75"

exec (My_Variable + My_Expression)

To read the variable, use eval():

print eval (My_Variable)


p.s.  While it may not be an accpeted practice to do this, I have found
it quite useful.  At work, we are able to quickly develop automated
tests that will do bounds checking on various python classes and

binnc at wrote:

> Another newbie question...
> Is there a way to have a string represent a variable.
> For eaxample, I have a the following:
> <snip>
> serial_number = '3'
> print 'ip_address_serial_' + serial_number
> ip_address_serial_3
> Now I want to use the string ip_address_serial_3 to pull
> the value assigned to the variable of the same name.
> How can I represent the string as a variable?
> Thanks in advance!
> Sent via

More information about the Python-list mailing list