我遇到了以下python代码,其中一个类从两个父类继承。我试图了解该类的构造函数。
# 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
的对象的过程是什么?
在呼叫EClient.__init__(self, self)
中,第一个self
变为EClient
的EClient
中的def __init__(self, wrapper):
。接下来,您可能会看到,第二个self
被绑定到该调用中的wrapper
。 TestApp
继承了EWrapper
,因此将其用作wrapper
的EClient
。
初始化TestApp
时,先使用self
的EWrapper
,然后使用EClient
,因为这是该类中定义的顺序。