1

I have some code snippet of code I'd like to abstract to a function that only has one small change that needs to be dynamic

if myUser.profile.get_setting_c == True :
# below does not work but you get the idea, how 
if myUser.profile.eval('get_setting_c') == True :
user391986
  • 29,536
  • 39
  • 126
  • 205

2 Answers2

4

Is this what you want?

getattr(myUser.profile, 'get_setting_c')

BTW, using eval is considered bad practice in python, see Is using eval in Python a bad practice? .

Community
  • 1
  • 1
satoru
  • 31,822
  • 31
  • 91
  • 141
  • Just to complete: assign to a variable like "attr = getattr(myUser.profile, 'get_setting_c'); if attr == True:" – diofeher Dec 20 '11 at 02:22
-1

Why not

if eval('myUser.profile.get_setting_c') == True:

or

def fun(setting):
    return eval('myUser.profile.%s' % setting)

if fun('get_setting_c') == True:

?

Zhack
  • 96
  • 7