python super
python super() 函数显式的引用父类, 它用来在子类中调用我们继承的超类的方法
要了解python super(),您必须了解Python继承。 在Python继承中,子类继承自超类。
super() 允许我们隐式引用超类。因此,Python super使我们的任务更简单、更舒适。在从子类引用超类时,我们不需要显式地写超类的名称。在接下来的几节中,我们将讨论python超函数。
example
class Person:# initializing the variablesname = ""age = 0# defining constructordef __init__(self, person_name, person_age):self.name = person_nameself.age = person_age# defining class methodsdef show_name(self):print(self.name)def show_age(self):print(self.age)# definition of subclass starts hereclass Student(Person):studentId = ""def __init__(self, student_name, student_age, student_id):Person.__init__(self, student_name, student_age)self.studentId = student_iddef get_id(self):return self.studentId # returns the value of student id# end of subclass definition# Create an object of the superclassperson1 = Person("Richard", 23)# call member methods of the objectsperson1.show_age()# Create an object of the subclassstudent1 = Student("Max", 22, "102")print(student1.get_id())student1.show_name()'''D:\projects\pythoncode\pythonparallel(pythonparallel-jU7W1wcf) λ python pysuperdemo.py23102MaxD:\projects\pythoncode\pythonparallel'''
上面的例子中, 我们调用父类的方法
Person.__init__(self, student_name, student_age)
我们可以这样替换
# definition of subclass starts hereclass Student(Person):studentId = ""def __init__(self, student_name, student_age, student_id):# Person.__init__(self, student_name, student_age)super().__init__(student_name, student_age)self.studentId = student_iddef get_id(self):return self.studentId
python3 super
注意,上面的语法是针对python3 super()的。如果你用的是python2.x版本,然后它略有不同,你将不得不做以下更改
class Person(object): # 2.x 必须继承自 object...super(Studnet, self).__init__(studnet_name, student_age)
2.x 版本的object 是必须的,否则会抛出以下错误
Traceback (most recent call last):File "super_example.py", line 40, in <module>student1 = Student("Max", 22, "102")File "super_example.py", line 25, in __init__super(Student, self).__init__(student_name, student_age)TypeError: must be type, not classobj
第二个变化是,super语法的变化,python3 super 语法更加简洁。
多继承中的 super
如前所述,super()允许我们隐匿引用超类。但是在多继承中,它将引用哪个类呢?super() 总是引用直接的超类。它不仅可以引用 __init__() ,还可以引用超类的所有其他函数。
class A:def __init__(self):print('Initializing: class A')def sub_method(self, b):print('Printing from class A:', b)class B(A):def __init__(self):print('Initializing: class B')super().__init__()def sub_method(self, b):print('Printing from class B:', b)super().sub_method(b + 1)class C(B):def __init__(self):print('Initializing: class C')super().__init__()def sub_method(self, b):print('Printing from class C:', b)super().sub_method(b + 1)if __name__ == '__main__':c = C()c.sub_method(1)'''D:\projects\pythoncode\pythonparallel(pythonparallel-jU7W1wcf) λ python pysuperdemo.pyInitializing: class CInitializing: class BInitializing: class APrinting from class C: 1Printing from class B: 2Printing from class A: 3D:\projects\pythoncode\pythonparallel'''
因此,从输出中我们可以清楚地看到,C类的 __init__() 函数首先被调用,然后是B类,然后是a类。
功能
我们为什么需要 super()
如果您以前有过Java语言的经验,那么您应该知道基类也是由超级对象调用的。 因此,这个概念实际上对编码人员有用。 但是,Python还为程序员保留了使用超类名称来引用它们的功能。 而且,如果你的程序包含多级继承,那么这个super()函数对你有帮助。
