我正在研究一個python項目,其中有一堆已創建的類。 我將要求用戶輸入要執行的類(即類的名稱,其列表將顯示給他)。如何通過列表創建一個類的實例
他選擇了一個選擇後,我想創建一個用戶請求的類的實例並顯示相應的輸出。
的情況是這樣的......
"""
My Class definitions go up here...
"""
classList = {'a':'AClass','b':'BClass','c':'CClass'}
for i in iter(classList):
print i
x = raw_input('Enter class name: ') 'Get the user input and store it.'
"""
I'm stuck here.
Have to create an instance for the requested class.
i.e., have to create an instance to AClass if the user entered a (classList[a])
"""
一個過程,我知道,但模糊的方法是使用交換機的情況下或者如果其他檢查輸入和對應的類創建實例。但是,我是python的新手,所以我想知道是否還有其他方法可以更優雅地實現這一點?
根據記錄,'「」」 ... 。「」「*不是python」block comment「*(儘管我已經多次使用過這種方式)。它會創建一個字符串以被丟棄,並可能導致縮進錯誤。 – mgilson