python如何查看父类

python如何查看父类,第1张

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

使用语法:类名.bases

举例说明 (推荐学习:Python视频教程)

举例:定义三个类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如何查看父类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1184397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存