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! ! !
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)
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:
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!