繁体   English   中英

Python中多个Inheritance的初始化

[英]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成为EClientdef __init__(self, wrapper):中的EClient 接下来,如您所见,第二个self在该调用中绑定到wrapper TestApp继承EWrapper ,因此它将自己用作EClientwrapper

初始化TestApp时,您使用的是EWrapperself ,然后是EClient ,因为这是 class 中定义的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM