在 python 中,super(classItSelf) 在做什么?

In python, what is super(classItSelf) doing?

在python中,super(classItSelf)在做什么?

我有一个项目要处理,代码如下:

class Tele(TcInterface):

    _signal = signal.SIGUSR1

    def __init__(self):
        super(Tele, self).__init__()
        self._c = None

这条线是做什么的?

super(Tele, self).__init__()

我是 cpp java 代码,真的对 python 东西感到困惑。

这会调用 super/parent class (TclInterface) 的 __init__() 函数(构造函数)。这通常是必要的,因为 superclass 构造函数会被 subclass.

中的 __init__() 覆盖。

您至少需要将 self 传给 __init__

python 不同于一些传统的 OOP 语言,它会自动处理 selfthis 或您为当前实例指定的任何名称。

在python中,self是暴露的,甚至在构造函数中,所以你需要自己传入并引用它。而这里的这个super(...),它基本上是在寻找一个代理,它代表所有超级类,而super(...).__init__是从超级类引用构造函数。它与您在 C++ 中调用 super() 或 Java.

的概念相同

更多请参考文档:https://docs.python.org/2/library/functions.html#super