2

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())")
4

1 に答える 1

10

What you're looking for is the getattr() built-in function. There is also hasattr() and setattr().

button = getattr(self, 'flipButton')
button.setEnabled(True)
于 2012-08-24T14:31:19.403 に答える