Python super() 函数

实例

创建一个将从另一个类继承所有方法和属性的类:

  1. class Parent:
  2. def __init__(self, txt):
  3. self.message
  4.  
  5. def printmessage(self):
  6. print(self.message)
  7.  
  8. class Child(Parent):
  9. def __init__(self, txt):
  10. super().__init__(txt)
  11.  
  12. x = Child("Hello, and welcome!")
  13.  
  14. x.printmessage()

定义和用法

super() 函数用于提供对父类或同胞类的方法和属性的访问。

super() 函数返回代表父类的对象。

语法

  1. super()

参数值

无参数