Pet*_*gru 0 python oop variables methods list
我正在制作一个类似字典的应用程序,根据翻译开关,屏幕显示翻译或原始单词代码如下所示:
PS:“textFunction(text)”只是一个在屏幕上显示文本的函数。PS:“wordList”是Word Class实例的列表
class Word:
def __init__(self, original, translation):
self.original = original
self.translation = translation
translation = False
if translation == False:
wordDisplayed = .original
elif translation == True:
wordDisplayed = .translation
textFunction(wordList[X].wordDisplayed)
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个语法错误。我该如何编码?
你会使用getattr:
wordDisplayed = "translation" if translation else "original"
textFunction(getattr(wordList[X], wordDisplayed))
Run Code Online (Sandbox Code Playgroud)