How to view parent class in python

(*-*)浩
Release: 2019-10-31 13:15:04
Original
5050 people have browsed it

Python provides a bases attribute for all classes, through which you can view all direct parent classes of the class. This attribute returns a tuple composed of all direct parent classes. Note that it is the direct parent class! ! !

How to view parent class in python

Usage syntax: class name.bases

Examples (Recommended learning: Python video tutorial)

Example: Define three classes: Vehicle, Automobile, and Car. In order to illustrate the problem, Car is set to inherit from the two classes Vehicle and Automobile, and Automobile Inherit from Vehicle. The class definition is as follows:

class Vehicle():
   def __init__(self,wheelcount):
       self.wheelcount = wheelcount
  
class Automobile(Vehicle):
      def __init__(self,wheelcount,power):
          self.power,self.totaldistance = '燃油发动机',0
          super().__init__(wheelcount) 
          
class Car(Automobile,Vehicle):  
    def __init__(self,wheelcount, power,oilcostperkm):
        self.oilcostperkm = oilcostperkm
        super().__init__(wheelcount, power)
Copy after login

Let’s look at the __bases__ of these three classes and draw the following conclusions:

The direct parent classes of Car. are Automobile and Vehicle;

The direct parent class of Automobile is Vehicle;

The direct parent class of Automobile is object.

The specific execution screenshots are as follows:

How to view parent class in python

How to view parent class in python

The above is the detailed content of How to view parent class 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!