打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python获取对象信息之内置函数dir()

    对于类对象实例对象,可以调用内置函数dir()获取其所有可以访问的属性和方法(包括从父类中继承的属性和方法)的列表。类对象与实例对象的结果是有区别的,类对象的结果不包括实例属性。

    示例:

#coding=gbk

class A(B):

    def __init__(self,x):

        self.x=x

        self.y=15

    def aa(self):

        pass

    def cc(self):

        self.j=0

        pass

    def bb(self):

        pass

ali=A(20)

print(dir(ali)) #也可以写成print(dir(A(10)))

print(dir(A)) 

    执行结果:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc', 'x', 'y']

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc']

    从执行结果看出,该函数查询实例时可以列出实例对象的所有属性和方法,但cc函数中的f属性不显示。查询类时可以列出对象的所有方法,但不列出属性。

    类似__xxx__的属性和方法在Python中都是有特殊用途的,比如__len__方法返回长度。在Python中,如果你调用len()函数试图获取一个对象的长度,实际上,在len()函数内部,它自动去调用该对象的__len__()方法,所以,下面的代码是等价的:

 >>> len('ABC')

>>> 'ABC'.__len__()

    如果在有继承的关系下示例如下:

#coding=gbk 

class B():

    def __init__(self,z):

        self.z=z

        self.w=1

    def ee(self):

        pass 

class A(B):

    def __init__(self,x):

        self.x=x

        self.y=15

    def aa(self):

        pass

    def cc(self):

        self.j=0

        pass

    def bb(self):

        pass

ali=A(20)

print(dir(ali)) #也可以写成print(dir(A(10)))

print(dir(A)) 

    执行结果如下:

     

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc', 'ee', 'x', 'y']

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc', 'ee']

    从执行结果可看出:

    该函数查询实例时可以列出实例对象的所有属性和方法,以及父类中的所有方法,但不列出父类的属性,查询类时可以列出对象及父类的所有方法,但不列出属性。

    将子类加入super().__init__()函数引入父类的构造函数

#coding=gbk 

class B():

    def __init__(self,z):

        self.z=z

        self.w=1

    def ee(self):

        pass 

class A(B):

    def __init__(self,x):

        super().__init__(2)

        self.x=x

        self.y=15

    def aa(self):

        pass

    def cc(self):

        self.j=0

        pass

    def bb(self):

        pass

ali=A(20)

print(dir(ali)) #也可以写成print(dir(A(10)))

print(dir(A)) 

执行结果如下:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc', 'ee', 'w', 'x', 'y', 'z']

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'aa', 'bb', 'cc', 'ee']

从执行结果可看出,加入父类的构造函数后, 该函数查询实例时可以列出实例对象的所有属性和方法,以及父类中的所有方法及属性。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
盘点Python 面向对象编程最容易被忽视的知识点
原来Python函数只是个对象
下划线用法
4个使阅读 Python 代码更容易的函数
你需要了解的最重要的Python概念
python函数property例解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服