2016-01-24 56 views
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) 
+1

你能提供一些樣品的輸入和輸出? – TigerhawkT3

+0

你可以繼承dict類型,你只需要擔心實現你的類的特定功能 – Copperfield

回答

0

我怎麼能有這樣的人的關鍵特性,不得不說是該值的屬性?

def __init__(self, dictonaryPerhaps): 
    self.__dict__.update(dictionaryPerhaps) 

如果您想通過一切爲關鍵字argumrnets到__init__

def __init__(self, **kwargs): 
    self.__dict__.update(**kwargs)