Home > Backend Development > Python Tutorial > Code example of how a subclass calls a parent class function in python

Code example of how a subclass calls a parent class function in python

黄舟
Release: 2017-08-20 10:51:30
Original
1315 people have browsed it

The initialization method of a class in Python is __init__(), so the initialization method of parent and subclasses is this. The following article mainly introduces you to examples of how subclasses call parent class functions in Python. The article introduces it in detail through sample code. Friends in need can refer to it.

Preface

This article mainly introduces to you the relevant content about calling parent class functions in Python subclasses. _ in subclasses in Python The _init__() function will override the parent class function. In some cases, it is often necessary to call the parent class function in the subclass. Not much to say below, let’s take a look at the detailed introduction:

In the following routine,? ? ? This is where the parent class function needs to be called. Next, we will introduce it in detail with the routine.


 # -*- coding:utf-8 -*-
 class Student:
  def __init__(self,name):
   self.name=name
  def ps(self):
   print('I am %s'%self.name)
 
 class Score(Student):
  def __init__(self,name,score):
   self.score=score
   ???12 12  def ps1(self):
   print('I\'m %s,%s' %(self.name,self.score))
 
 Score('Bob','99').ps()
 Score('Bob','99').ps1()
Copy after login

In Python3.5, by consulting the information, there are several calling methods as follows.

The first is the direct method. Use the parent class name to call directly, in the form of parent_class.parent_attribute(self) , the corresponding routine is the statement:


Student.__init__(self,name)
Copy after login

The second is through super Function, in the shape of super(child_class, child_object).parent_attribute(arg). The first parameter indicates the starting point of calling the parent class, and the second parameter indicates the class instance (usually self is used). When the parameter of the parent class method is only self, the parameter args does not need to be written. In addition, when used inside a class, child_class, child_object can also be omitted. Corresponding routine:


super(Score,self).__init__(name)
Copy after login

or:


super().__init__(name)
Copy after login

super## can also be used outside the class #Function, but it must have two parameters child_class, child_object.

Summarize

The above is the detailed content of Code example of how a subclass calls a parent class function in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template