python - 为什么 super().__init__ 没有自引用?

标签 python python-3.x oop super

<分区>

为什么我们在使用 super().__init__ 时不需要自引用?(例如下面的第 9 行)

class labourers():
    def __init__(self,name,department,salary):
        self.name = name
        self.department = department
        self.salary = salary

class managers(labourers):
    def __init__(self,name,department,salary,numberofpeople):
        super().__init__(name,department,salary)
        self.numberofpeople = numberofpeople

最佳答案

在这种情况下,Super 的功能是在 CPython 解析器中实现的。参见 PEP 3135

Replacing the old usage of super, calls to the next class in the MRO (method resolution order) can be made without explicitly passing the class object (although doing so will still be supported). Every function will have a cell named __class__ that contains the class object that the function is defined in.

The new syntax:

super()

is equivalent to:

super(__class__, <firstarg>)

[...]

While super is not a reserved word, the parser recognizes the use of super in a method definition and only passes in the __class__ cell when this is found. Thus, calling a global alias of super without arguments will not necessarily work.

添加了强调。

关于python - 为什么 super().__init__ 没有自引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61261992/

相关文章:

python - 为什么 tensorflow 模型总是预测同一类?

Python避免在生成器中重复调用函数

python - 如何为 matplotlib 图例命名

python - 使用自定义编码器将带有 Decimal 键的 python 字典转换为 JSON

php - 哪个类应该取决于哪个类基于重要性级别

javascript - 将简单的实用对象合并到更复杂的专用对象中的最佳方法是什么?

python - 测试numpy数组中的每个元素是否位于两个值之间的简单方法?

sql - 连续对某行进行pandas列操作

python - 在点击命令之上装饰

javascript - JavaScript 中的 "undefined x 1"是什么?