[英]Initialization of Multiple Inheritance in Python
我遇到了以下 python 代码,其中 class 继承自两个父类。 我试图了解 class 的构造函数。
# wrapper.py:
#############
class EWrapper:
def __init__(self):
pass
...
# client.py
###########
class EClient(object):
def __init__(self, wrapper):
self.msg_queue = queue.Queue()
self.wrapper = wrapper
self.decoder = None
self.reset()
....
# Test.py
#########
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
有人可以更好地了解EClient.__init__(self, self)
吗? 我不清楚两个self
的用法。 python怎么知道哪个self
是哪个?
TestApp
的 object 的构建过程是怎样的?
在调用EClient.__init__(self, self)
中,第一个self
成为EClient
的def __init__(self, wrapper):
中的EClient
。 接下来,如您所见,第二个self
在该调用中绑定到wrapper
。 TestApp
继承EWrapper
,因此它将自己用作EClient
的wrapper
。
初始化TestApp
时,您使用的是EWrapper
的self
,然后是EClient
,因为这是 class 中定义的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.