2011-12-20 2 views
0

내가 난 단지장고 파이썬 평가

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 : 

답변

4

동적 할 필요가 하나의 작은 변화가 함수에 추상적 싶습니다 코드의 일부 코드를 가지고 있습니까? eval를 사용 BTW

getattr(myUser.profile, 'get_setting_c')

는, 파이썬에서 나쁜 사례로 간주되고, Is using eval in Python a bad practice?를 참조하십시오.

+0

"attr = getattr (myUser.profile, 'get_setting_c')과 같은 변수에 지정 : attr == True :" – diofeher

0

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

또는

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

if fun('get_setting_c') == True: 

?