I write the test code with 3 classes , and using Chain of Responsibility design pattern , the code below
and I print print(c._abc is b._abc)
, the answer is True , but my original think is that the two are different.
Then , Round 2 , I uncomment self._abc = kwargs
and comment other 3 lines ,
the answer become False .
Why is it so?
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()