Forum Home
Press F1
 
Thread ID: 127179 2012-10-08 22:42:00 Python variables Mike (15) Press F1
Post ID Timestamp Content User
1305864 2012-10-08 22:42:00 Hi all,

I have a python script that grabs values from a SQL table. I need to reference a variable based on the value in the SQL record.

So if the value returned is "Mike", I want to reference the variable called Mike (and whatever value is attached to variable Mike).



# variables
mike = "Hello"
bob = "Goodbye"

variableFromSQL = "mike"

print <value based on variable referenced in variableFromSQL>


which would give the result
Hello

Does that make sense? Is this possible? I'm not trying to create a variable, just reference an existing variable based on another variable's value. I could do this with an if elif etc., but since there are 40+ possible values it will just get a bit messy.

Cheers,
Mike.
Mike (15)
1305865 2012-10-08 22:52:00 Nevermind, figured it out right after I posted the question :)


print vars()[variableFromSQL]

Cheers,
Mike.
Mike (15)
1