的Python - 困惑继承

我写测试代码3类,并使用责任设计链模式,下面的Python - 困惑继承

我打印print(c._abc is b._abc)代码,答案是真的,但我原来认为,这两个是不同的。

然后,第2轮,我取消注释self._abc = kwargs并评论其他3行, 答案变成False。

这是为什么?

import abc 

class A:

__metaclass__ = abc.ABCMeta

_abc = {}

def __init__(self,successor=None,**kwargs):

self._successor = successor

@abc.abstractmethod

def handlerRequest(self):

pass

class B(A):

def __init__(self,successor=None,**kwargs):

self._successor = successor

print(kwargs)

# self._abc = kwargs # round 2<---uncomment here

self._abc['a'] = kwargs['a'] # round 2<---comment here

self._abc['b'] = kwargs['b'] # round 2<---comment here

self._abc['Hello'] = 'World' # round 2<---comment here

def handlerRequest(self):

if (self._successor is not None):

self._successor.handlerRequest()

print(self._abc)

class C(A):

def handlerRequest(self):

if (self._successor is not None):

self._successor.handlerRequest()

print(self._abc)

list = {'a':1,'b':2}

b = B(**list)

c = C(b)

print(c._abc is b._abc)

c.handlerRequest()

以上是 的Python - 困惑继承 的全部内容, 来源链接: utcz.com/qa/259643.html

回到顶部