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 :
2 个回答
-1
为什么不呢
if eval('myUser.profile.get_setting_c') == True:
或者
def fun(setting):
return eval('myUser.profile.%s' % setting)
if fun('get_setting_c') == True:
?
4
这就是你想要的吗?
getattr(myUser.profile, 'get_setting_c')
顺便说一下,在Python中使用 eval
被认为是不好的做法,详细内容可以查看这个链接:在Python中使用eval是不好的做法吗?。