【发布时间】:2012-08-20 03:12:14
【问题描述】:
有没有办法在 python 中使用变量字符串来访问变量?例如,我想要一种比使用eval 更简洁的方法:
def toggleListButtons (self):
buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"]
for button in buttons:
eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())")
【问题讨论】:
-
+1 好问题,因为 eval 是邪恶的
标签: python variables eval variable-variables