Python super() 函数允许我们明确地参考母类,在我们想要调用超级类函数的继承情况下,它很有用。
超级 Python
要了解 Python 超级函数,你必须知道关于 Python 继承。在 Python 继承中,子类继承来自超级班级。 Python 超级班级() 函数允许我们暗示性地参考超级班级。因此, Python 超级使我们的任务更容易和舒适。在从子班级引用超级班级时,我们不需要明确地写下超级班级的名称。
Python 超级函数示例
首先,只需看看我们在我们的 Python 继承教程中使用的以下代码。 在这个示例代码中,超级类是人
和子类是学生
。
1class Person:
2 # initializing the variables
3 name = ""
4 age = 0
5
6 # defining constructor
7 def __init__(self, person_name, person_age):
8 self.name = person_name
9 self.age = person_age
10
11 # defining class methods
12
13 def show_name(self):
14 print(self.name)
15
16 def show_age(self):
17 print(self.age)
18
19# definition of subclass starts here
20class Student(Person):
21 studentId = ""
22
23 def __init__(self, student_name, student_age, student_id):
24 Person.__init__(self, student_name, student_age)
25 self.studentId = student_id
26
27 def get_id(self):
28 return self.studentId # returns the value of student id
29
30# end of subclass definition
31
32# Create an object of the superclass
33person1 = Person("Richard", 23)
34# call member methods of the objects
35person1.show_age()
36# Create an object of the subclass
37student1 = Student("Max", 22, "102")
38print(student1.get_id())
39student1.show_name()
在上面的例子中,我们将父母类函数称为:
1Person.__init__(self, student_name, student_age)
我们可以用下面的 python 超级函数调用来代替。
1super().__init__(student_name, student_age)
The output will remain the same in both the cases, as shown in the below image.
Python 3 超级
请注意,上述语法适用于 python 3 超级函数. 如果您使用的是 python 2.x 版本,则略有不同,您将不得不做出以下更改:
1class Person(object):
2...
3 super(Student, self).__init__(student_name, student_age)
第一个更改是将object
作为Person的基本类别,需要在Python 2.x版本中使用超级函数,否则你会收到以下错误。
1Traceback (most recent call last):
2 File "super_example.py", line 40, in <module>
3 student1 = Student("Max", 22, "102")
4 File "super_example.py", line 25, in __init__
5 super(Student, self).__init__(student_name, student_age)
6TypeError: must be type, not classobj
正如你可以看到的,python 3超级函数的语法更容易使用,语法也看起来干净。
Python 超级函数与多层次继承
正如我们之前所述的,Python super()函数允许我们暗示地引用超级阶级,但在多层次继承的情况下,它会引用哪个类?好吧,Python super()总是会引用即时的超级阶级。
1class A:
2 def __init__(self):
3 print('Initializing: class A')
4
5 def sub_method(self, b):
6 print('Printing from class A:', b)
7
8class B(A):
9 def __init__(self):
10 print('Initializing: class B')
11 super().__init__()
12
13 def sub_method(self, b):
14 print('Printing from class B:', b)
15 super().sub_method(b + 1)
16
17class C(B):
18 def __init__(self):
19 print('Initializing: class C')
20 super().__init__()
21
22 def sub_method(self, b):
23 print('Printing from class C:', b)
24 super().sub_method(b + 1)
25
26if __name__ == '__main__':
27 c = C()
28 c.sub_method(1)
让我们来看看上面的 python 3 超级示例与多层次继承的输出。
1Initializing: class C
2Initializing: class B
3Initializing: class A
4Printing from class C: 1
5Printing from class B: 2
6Printing from class A: 3
因此,从输出中我们可以清楚地看到,C类的__init__()
函数首先被调用,然后是B类,然后是A类。
为什么我们需要Python超级函数
如果你以前有 Java 语言的经验,那么你应该知道,基础类也被一个 super 对象称呼,所以,这个概念实际上对编码器很有用,但是,Python 也保留了程序员使用超级类名称来引用它们的能力,如果你的程序包含多层次的继承,那么这个 super() 函数对你很有帮助。
您可以从我们的 GitHub 存储库中查阅完整的 Python 脚本和更多 Python 示例。
参考: 官方文件