Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to reuse the function of the parent class by the subclass of python

Shulou Source: shulou.com Published: 2022-05-31 10:47:02 09月21日 Update

This article mainly introduces the knowledge of "how do subclasses of python reuse parent class functions". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to reuse parent class functions of python subclasses" can help you solve the problem.

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', 18meme 'unlimited', 10Power9000) print (tea_obj.__dict__) on "how the subclasses of python reuse the functions of the parent class". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Function subclass knowledge object attribute method industry different practical special code content function name instance practicality actual effect article more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Apple Shulou Tech Info Huawei