I've had this problem in the past and never found the solution. I've checked ton's of google links and still don't know. What I want to do is use a string as a variable. I'm working with SQLalchemy so will use the example straight from my project: (look for the variable 'objective' in the function)
Here's an example:
def win_ratio_p_obj(objective):
    #want to find the win/loss ratio for each obj_first, ie. 60% of times when team gets fblood you also win vs. 40% of time you lose
    obj_totals = session.query(Match.win, func.count(Match.win)).filter(Match.**objective** == 't').group_by(Match.win).order_by(Match.win).all()
    win_chance = obj_totals[1][1]/(obj_totals[0][1]+obj_totals[1][1])
    return win_chance
objective = 'first_dragon'    
x = win_ratio_p_obj(objective)
objective = 'first_blood' 
y = win_ratio_p_obj(objective)
objective = 'first_turret' 
z = win_ratio_p_obj(objective)
objective = 'first_inhib'    
Returns:
Traceback (most recent call last):
  Python Shell, prompt 15, line 1
builtins.AttributeError: type object 'Match' has no attribute 'objective'
So what I want to do is use each objective as a variable name with the aim of reducing code repetition. I know I could very easily copy paste the function a few times but that seems silly. At the moment the code above won't recognise the objective variables values as variables instead of strings.
Any answers will be super well appreciated!


getattr(Match, objective).