我们如何将属性从 child 传递到 parent?

How can we pass an attribute from child to parent?

我正在尝试将列表从 child 传递到 parent。 在这里我期望 class IVone92c 应该将其属性 candidateList 传递给 IVone83c 并且它应该在自己的列表中得到相同的属性。相反,object 被附加到 IVone92c 的 candidateList 中。为什么会这样?我怎样才能达到我想要的输出?

class IVone83c(Object):
    def __init__(self):
        self.candidateList = list()

    def resolve(self, SabxarUpa, candiList):
        self.candidateList.extend(candiList)


class IVone92c(IVone83c):

    def check(self,SabxarUpa, rule=None):
            self.candidateList = [IVone92]
             super(IVone92c, self).resolve(SabxarUpa, self.candidateList)

Why does this happen?

class IVone83c(object):
    def __init__(self):
        self.candidateList = ['goodbye']

    def resolve(self, SabxarUpa, candiList):
        print self.candidateList
        print self


class IVone92c(IVone83c):

    def check(self,SabxarUpa, rule=None):
        self.candidateList = [IVone92c]
        super(IVone92c, self).resolve(SabxarUpa, self.candidateList)

iv92 = IVone92c()
print iv92

--output:--
<__main__.IVone92c object at 0x2bdc30>  #<--Note the id
[<class '__main__.IVone92c'>]
<__main__.IVone92c object at 0x2bdc30>  #<--Note the id

super(type[, object-or-type])

Return a proxy object that delegates method calls to a parent...

If the second argument is omitted, the super object returned is unbound.

相反,如果提供了第二个参数,则超级对象绑定到第二个参数。 super() 调用中的第二个参数是 IVone92c_instanceBound表示super()将IVone92c_instance作为第一个参数传递给父方法,也就是将IVone92c_instance赋值给父方法中的self参数变量方法,这意味着在父方法 self 内部是 IVone92c_instance.

How can I achieve my desired output?

不清楚您想要的输出是什么。你说:

Here I am expecting class IVone92c should pass its attribute candidateList to IVone83c

当然,这没有任何意义。您要将列表传递给 class?那是什么意思?你期望发生什么?正如 jonrsharpe 在评论中提到的,您可以将数据存储在 class 属性中。那是你想做的吗?

或者,您是否想以某种方式创建父实例 class,然后更新其列表,然后让父实例奇迹般地存活下来,直到您准备好检索它?

class IVone83c(object):
    def __init__(self):
        self.candidateList = ['goodbye']

    def resolve(self, SabxarUpa, candiList):
        #print(self.candidateList)
        print(self)
        self.candidateList.extend(candiList)


class IVone92c(IVone83c):

    def check(self,SabxarUpa, rule=None):
        self.candidateList = [IVone92c]
        iv83 = IVone83c()
        iv83.candidateList.extend(self.candidateList)
        self.candidateList = iv83.candidateList[:]
        print iv83.candidateList
        print self.candidateList
        #Where do you want to save iv83?

iv92 = IVone92c()
iv92.check('hello')

--output:--
['goodbye', <class '__main__.IVone92c'>]
['goodbye', <class '__main__.IVone92c'>]