PDA

View Full Version : Python variables



Mike
09-10-2012, 11:42 AM
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
09-10-2012, 11:52 AM
Nevermind, figured it out right after I posted the question :)


print vars()[variableFromSQL]

Cheers,
Mike.