在python中继承时出错

时间:2014-01-07 06:48:46

标签: python

我正在使用一个名为IPKISS的不为人知的框架;希望这没关系。

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)

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

你必须在你的 ElectrodeStructure 类中添加__init__方法,正如@ hd1指出的那样 - 必须设置金额

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

你调用ElectrodePair .__ init__的方式是错误的,因为在你的班级中没有ElectrodeStructure .__ init__,前者将被自动调用

编辑:

我在重新阅读时注意到的一些事情 - 你从一个类继承,然后在一个类方法中创建一个父类的对象。这里出了点问题

答案 1 :(得分:0)

class ElectrodeStructure(ElectrodePair):
    [...]

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

ElectrodeStructure中创建新main()时,您传递的是keword参数。因为你没有在__init__中定义ElectrodeStructure函数,所以使用这些参数调用超级__init__(包括amount,所以没有错误)。

然后在define_elements中,您再次调用__init__,除非您没有传入任何引发错误的参数。

此外,这句话:

        electrodePair = ElectrodePair.__init__(self)

None返回值(可能ElectrodePair.__init__(self))分配给electrodePair。我怀疑你想要更像以下内容:

        electrodePair = ElectrodePair(amount=1, etc.)

看起来你想要成分,而不是继承;您可以在正确的__init__函数中初始化子类(如@volcano描述的那样),或者只创建一个ElectrodePair类成员,而不是继承。