ers*_*ski 3 python oop super python-3.x
为什么我们使用的时候不需要自引用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
Run Code Online (Sandbox Code Playgroud)
在本例中,Super 的功能是在 CPython 解析器中实现的。参见PEP 3135
取代 super 的旧用法,可以在不显式传递类对象的情况下调用 MRO(方法解析顺序)中的下一个类(尽管仍然支持这样做)。每个函数都有一个名为 __class__ 的单元格,其中包含定义该函数的类对象。
新语法:
Run Code Online (Sandbox Code Playgroud)super()相当于:
Run Code Online (Sandbox Code Playgroud)super(__class__, <firstarg>)[...]
虽然 super 不是保留字,但解析器会识别方法定义中 super 的使用,并且仅在找到它时才传入 __class__ 单元格。因此,不带参数调用 super 的全局别名不一定有效。
添加了强调。
| 归档时间: |
|
| 查看次数: |
922 次 |
| 最近记录: |