2011-12-20 4 views
0

は私が唯一DjangoのPythonのevalの

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

動的にする必要があります一つの小さな変化が機能に抽象化したいのですが、コードのいくつかのコードスニペットを持っていますか?

getattr(myUser.profile, 'get_setting_c')

ところで、evalを使用すると、Is using eval in Python a bad practice?を参照して、Pythonで悪い習慣と考えられています。

+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: