python中super,python3 super_Python super()– Python 3 super()

 2023-11-19 阅读 26 评论 0

摘要:python3 superPython super() function allows us to refer to the parent class explicitly. It’s useful in case of inheritance where we want to call super class functions. Python super()函數允許我們顯式引用父類。 在繼承的情況下,我們要調用

python3 super

Python super() function allows us to refer to the parent class explicitly. It’s useful in case of inheritance where we want to call super class functions.

Python super()函數允許我們顯式引用父類。 在繼承的情況下,我們要調用超類函數很有用。

超級Python (Python super)

To understand about python super function, you have to know about Python Inheritance. In Python Inheritance, the subclasses inherit from the superclass.

要了解python超級功能,您必須了解Python繼承 。 在Python繼承中,子類從超類繼承。

Python super() function allows us to refer the superclass implicitly. So, Python super makes our task easier and comfortable. While referring the superclass from the subclass, we don’t need to write the name of superclass explicitly. In the following sections, we will discuss python super function.

Python super()函數允許我們隱式引用超類。 因此,Python super使我們的任務更加輕松和舒適。 從子類中引用超類時,我們不需要顯式地編寫超類的名稱。 在以下各節中,我們將討論python超級功能。

Python超級函數示例 (Python super function example)

At first, just look at the following code we used in our Python Inheritance tutorial. In that example code, the superclass was Person and the subclass was Student. So the code is shown below.

首先,請看一下我們在Python繼承教程中使用的以下代碼。 在該示例代碼中,超類為Person ,子類為Student 。 因此,代碼如下所示。

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 here
class 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 superclass
person1 = Person("Richard", 23)
# call member methods of the objects
person1.show_age()
# Create an object of the subclass
student1 = Student("Max", 22, "102")
print(student1.get_id())
student1.show_name()

In the above example, we have called parent class function as:

在上面的示例中,我們將父類函數稱為:

Person.__init__(self, student_name, student_age)

We can replace this with python super function call as below.

我們可以將其替換為python超級函數調用,如下所示。

super().__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 super)

Note that the above syntax is for python 3 super function. If you are on python 2.x versions, then it’s slightly different and you will have to do the following changes:

請注意,以上語法適用于python 3超級功能。 如果您使用的是python 2.x版本,則略有不同,您將需要進行以下更改:

class Person(object):
...super(Student, self).__init__(student_name, student_age)

The first change is to have object as the base class for Person. It’s required to use the super function in Python 2.x versions. Otherwise, you will get the following error.

第一個更改是將object作為Person的基類。 在Python 2.x版本中需要使用超級功能。 否則,您將得到以下錯誤。

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

The second change in the syntax of the super function itself.

超函數本身語法的第二個變化。

As you can see that python 3 super function is a lot easier to use and the syntax is also clean looking.

如您所見,python 3超級函數易于使用,語法也很簡潔。

具有多級繼承的Python超函數 (Python super function with multilevel inheritance)

As we have stated previously that Python super() function allows us to refer the superclass implicitly.

如前所述,Python super()函數允許我們隱式引用超類。

But in the case of multi-level inheritances which class will it refer? Well, Python super() will always refer the immediate superclass.

但是在多級繼承的情況下,它將引用哪個類? 好吧,Python super()將始終引用直接超類。

Also Python super() function not only can refer the __init__() function but also can call all other function of the superclass. So, in the following example, we will see that.

Python 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)

Let’s see the output of above python 3 super example with multi-level inheritance.

讓我們看一下上面帶有多級繼承的python 3 super示例的輸出。

Initializing: class C
Initializing: class B
Initializing: class A
Printing from class C: 1
Printing from class B: 2
Printing from class A: 3

So, from the output we can clearly see that the __init__() function of class C had been called at first, then class B and after that class A. Similar thing happened by calling sub_method() function.

因此,從輸出中我們可以清楚地看到,首先調用了C類的__init__()函數,然后調用了B類,再調用了A類。通過調用sub_method()函數也發生了類似的情況。

為什么我們需要Python超級功能 (Why do we need Python super function)

If you have previous experience in Java language, then you should know that the base class is also called by a super object there. So, this concept is actually useful for the coders. However, Python also keeps the facility for the programmer to use superclass name to refer them. And, if your program contains multi-level inheritance, then this super() function is helpful for you.

如果您以前有Java語言的經驗,那么您應該知道基類也被那里的超級對象調用。 因此,這個概念實際上對編碼人員很有用。 但是,Python還為程序員保留了使用超類名稱來引用它們的便利。 而且,如果您的程序包含多級繼承,那么此super()函數將對您有所幫助。

So, that’s all about python super function. Hopefully, you understood this topic. Please use the comment box for any query.

所以,這一切都與python超級功能有關。 希望您了解此主題。 請對任何查詢使用注釋框。

GitHub Repository.GitHub存儲庫中檢出完整的python腳本和更多Python示例。

Reference: Official Documentation

參考: 官方文檔

翻譯自: https://www.journaldev.com/15911/python-super

python3 super

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183276.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息