0
我正在研究一個允許用戶創建類別的Python代碼,然後用他們想要的任意數量的對象填充它們。最後,我打印了所有內容,並使用File I/O創建了帶有信息的文本文檔。使用字典在Python中創建對象
因爲用戶可以輸入許多類別和飛機,所以我無法控制該類的init中的參數數量。我想創建一個字典,它爲作爲字典關鍵字的Airplane對象創建屬性,併爲這些屬性值創建值。我將如何做到這一點?
謝謝。
class Airplane:
def __init__(self, dictionaryPerhaps):
#How could I make properties that were the keys and had properties that were the values?
pass
def objectCreation(valuesOC, catagoriesOC):
thePlanes = { }
for each in catagoriesOC:
thePlanes [each] = valuesOC[each]
theAirplaneObject = Airplane(thePlanes)
你能提供一些樣品的輸入和輸出? – TigerhawkT3
你可以繼承dict類型,你只需要擔心實現你的類的特定功能 – Copperfield