How to reuse the function of the python subclass
This article mainly introduces the python subclass how to reuse the parent class function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
1. Call functions under a certain class directly according to the name, rather than relying on inheritance relationships.
2. Call the method mentioned by the parent class, that is, strictly rely on the inheritance relationship. Calling super results in a special object that refers to the mro of the class that initiated the property search and finds the property in the parent class of the current class.
Example
Class people: def _ _ init__ (self,name,age,sex): self.name=name self.age=age self.sex=sex class boss (people): def _ _ init__ (self,name,age,sex,level,salary): # the following two lines of code have the same effect: super (boss,self). _ _ init__ (name,age,sex) super (). _ _ init__ (name,age Sex) self.level = level self.salary=salary print (boss.mro ()) print ('='* 50) tea_obj=boss ('capitalist', 18'no limit', 10 'Magi 9000) print (tea_obj.__dict__) Thank you for reading this article carefully I hope the article "how to reuse the parent function of python subclasses" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!