python查看父类的方法

python查看父类的方法

python查看父类的方法?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

 Python 为所有类都提供了一个 bases 属性,通过该属性可以查看该类的所有直接父类,该属性返回所有直接父类组成的元组。注意是直接父类!!!

python查看父类的方法

使用语法:类名.bases

举例说明            

举例:定义三个类Vehicle(车)、Automobile(汽车)、Car(小汽车),为了说明问题,将Car设置为继承自Vehicle和Automobile两个类,而Automobile继承Vehicle。类定义如下:

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)

我们来查看这三个类的__bases__,得出结论如下:

Car.的直接父类是Automobile、Vehicle;

Automobile的直接父类是Vehicle;

Automobile的直接父类是object。

具体执行截屏如下:

python查看父类的方法

python查看父类的方法

感谢各位的阅读!看完上述内容,你们对python查看父类的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注行业资讯频道。