2014-01-07 69 views
0

我使用了一個叫做IPKISS的不爲人知的框架;希望這並不重要。在python中繼承時出錯

from ipkiss.all import *  

class ElectrodePair(Structure): 
    """An electrode component to be used for a PPLN design.""" 

    __name_prefix__ = "ELECTRODE_PAIR" 

    width = PositiveNumberProperty(required = True) 
    height = PositiveNumberProperty(required = True) 
    seperation = PositiveNumberProperty(required = True) 

    lay1 = Layer(number = 1, name = "boundaries") 

    def define_elements(self, elems): 
     left = ShapeRectangle(center = (-self.seperation*0.5,0.), box_size = (self.width, self.height)) 
     right = ShapeRectangle(center = (self.seperation*0.5,0.), box_size = (self.width, self.height)) 

     elems += Boundary(layer = self.lay1, shape = left) 
     elems += Boundary(layer = self.lay1, shape = right) 
     return elems 



class ElectrodeStructure(ElectrodePair): 
    """An array of electrodes.""" 

    __name_prefix__ = "ELECTRODE_STRUCTURE" 

    amount = PositiveNumberProperty(required = True) 
    spacing = PositiveNumberProperty(required = True) 

    def define_elements(self, elems): 
     electrodePair = ElectrodePair.__init__(self) 
     elems += ARefX(reference = electrodePair, origin = (0,0), period_1d = self.spacing, n_o_periods_1d = self.amount) 
     return elems 



def main(): 
    FILE_NAME = "ElectrodeArray.gds" 
    electrodeArray = ElectrodeStructure(amount = 10, height = 100., seperation = 20, spacing = 10., width = 2.) 

    electrodeArray.write_gdsii(FILE_NAME) 


if __name__ == "__main__": 
    main() 

我不知道爲什麼這是錯誤的。錯誤是:

File "/usr/local/lib/python2.7/dist-packages/IPKISS-2.4_ce-py2.7.egg/ipcore/properties/initializer.py", 
line 327, in __init__ raise IpcoreAttributeException("Required property '%s' is not found in keyword arguments of '%s' initialization." % (p, str(type(self)))) 
ipcore.exceptions.exc.IpcoreAttributeException: Required property 'amount' is not found in keyword arguments of '<class '__main__.ElectrodeStructure'>' initialization. 

它好像是不滿意我如何通過我的論點,我試過的東西堆和不能得到它的工作。建議將不勝感激。

我懷疑是因爲electrodePair = ElectrodePair.__init__(self)

謝謝你的時間。

+0

是的,你必須在你的ElectrodePair初始化器中指定一個數量 – hd1

回答

0
class ElectrodeStructure(ElectrodePair): 
    [...] 

    def define_elements(self, elems): 
     electrodePair = ElectrodePair.__init__(self) 
     [...] 

。因爲你沒有在ElectrodeStructure中定義__init__函數,所以超級的__init__被這些參數調用(包括amount,所以沒有錯誤)。

然後在define_elements,你再次打電話__init__,除非你沒有傳入任何參數,這是導致錯誤。

此外,這樣的說法:

 electrodePair = ElectrodePair.__init__(self) 

ElectrodePair.__init__(self)返回值(可能None)分配給electrodePair。我懷疑你想要更類似於以下的東西:

 electrodePair = ElectrodePair(amount=1, etc.) 

它看起來像你想組成,而不是繼承;你可以用適當的__init__函數初始化你的子類(如@volcano所描述的),或者只是創建一個ElectrodePair類成員,而不是繼承。

1

你有方法添加__init__您ElectrodeStructure類, - 正如@ HD1指出 - 必須設置

class ElectrodeStructure(ElectrodePair): 
    def __init__(self, amount): 
     ElectrodePair.__init__(self) 

你打電話ElectrodePair .__方式init__是錯誤的,因爲在你的類沒有ElectrodeStructure .__ init__前者將自動被調用

編輯:

夫婦的事情我已經NotI位重讀 - 從一個類繼承,然後在一個類方法中創建一個父類的對象。當您在main()ElectrodeStructure,你傳遞keword爭論什麼是錯在這裏