In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is to explain the basic inheritance of classes in python3 and the difference between self and super. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the basis of class inheritance in python3 and the difference between self and super".
Inheritance of classes in python:
The subclass inherits the parent class, and the subclass has the properties and methods of the parent class.
Classes in python are all initialized with _ _ init__ (). So both the parent class and the subclass are initialized with _ _ init__ (), but if the subclass does not initialize this function, it calls the parent class's _ _ init__ (); if this function is implemented, the parent class's initialization function is overridden. If you inherit the _ _ init__ () of the parent class, you need to display the call to this function in the subclass. The implementation is as follows:
Class Animal (object): def _ init__ (self): self.name = "I am the parent class" class Panda (Animal): def _ init__ (self): super (). _ _ init__ () # use super to display the _ _ init__ () function if _ _ name__== "_ main__" of the parent class: panda = Panda () # instantiate Panda print (panda.name) I am the parent class # output You can see that the initialization function of the parent class is used and the name attribute is present.
Subclasses can also define their own properties in the initialization function:
Class Animal (object): def _ init__ (self): self.name = "I am the parent" class Panda (Animal): def _ init__ (self): super (). _ _ init__ () self.myname = "panda" if _ name__== "_ main__": panda = Panda () print (panda.myname) panda # subclass's own attributes
The difference between self and super:
★ self calls its own method first if it no longer goes to the parent class; super looks for the method directly from the parent class
★ self is a class and super is a precompiled instruction
The output of ★ self class and super calss is the same
Class Animal (object): def _ init__ (self): self.name = "I am the parent class" def A (self): # A method print in the parent class ("A method of the parent class") class Panda (Animal): def _ init__ (self): super (). _ init__ () self.myname = "panda" def A (self): # A method print ("A side of the subclass" Def B (self): self.A () # self calls A super (). A () # super calls An if _ name__== "_ _ main__": panda = Panda () panda.B () # call A method through B function To see the difference between self and super, the A method of the subclass # We said that self first finds the method from itself, and no longer goes to the parent class to find the A method of the parent class, while super looks directly from the parent class.
If there is no A method in the subclass, it will output:
The A method of the parent class # there is no subclass, self looks for the A method of the parent class from the parent class
If there is no parent class, an error will be reported.
At this point, I believe you have a deeper understanding of "the basis of inheritance of classes in python3 and the difference between self and super". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 300
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.