python如何查看父类


本文摘自php中文网,作者(*-*)浩,侵删。

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

使用语法:类名.bases

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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-p-143.png

python-p-144.png

以上就是python如何查看父类的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中有关paramiko模块的学习分享

Python 2.7在linux下安装或升级的操作方法

Python 字典(dictionary)操作详解_Python

Python中new类方法和init 实例方法以及单例模式的介绍(附示例)

Python+selenium实现简易地疫情信息自动打卡签到功能

Python爬虫能做什么

Python 实现字符串中指定位置插入一个字符

Python的解释器是什么?

Python和c先学哪个

Python图像处理二值化方法实例云集

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...