2
有沒有辦法在Python中使用變量字符串來訪問變量?舉例來說,我想一個更合適的方法比使用eval
以下:沒有eval的Python變量變量?
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是邪惡的 – msw
有多邪惡?非常。 http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice – msw