pythondir什么意思_Python之dir()与__dict__的区别

首先需要知道的是,dir()是Python提供的一个API函数,dir()函数会自动寻找一个对象的所有属性,包括搜索__dict__中列出的属性。

不是所有的对象都有__dict__属性。例如,如果你在一个类中添加了__slots__属性,那么这个类的实例将不会拥有__dict__属性,但是dir()仍然可以找到并列出它的实例所有有效属性。

>>> classFoo(object):

...__slots__ = ('bar', )

... bar= 'spam'...>>> Foo.__dict__dict_proxy({'__module__': '__main__', 'bar': 'spam', '__slots__': ('bar',), '__doc__': None})>>> Foo().__dict__Traceback (most recent call last):

File"", line 1, in AttributeError:'Foo' object has no attribute '__dict__'

>>>dir(Foo)

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'bar']>>>dir(Foo())

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'bar']

同理许多内建类型都没有__dict__属性,例如list没有__dict__属性,但你仍然可以通过dir()列出list所有的属性。

>>> [].__dict__Traceback (most recent call last):

File"", line 1, in AttributeError:'list' object has no attribute '__dict__'

>>>dir([])

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

dir()函数和类实例

Python的实例拥有它们自己的__dict__,而它们对应的类也有自己的__dict__:

>>> classFoo(object):

... bar= 'spam'...>>> Foo().__dict__{}>>> Foo.__dict__dict_proxy({'__dict__': , '__weakref__': , '__module__': '__main__', 'bar': 'spam', '__doc__': None})

dir()函数会遍历Foo,Foo()和object的__dict__属性,从而为Foo类,它的实例以及它所有的被继承类创建一个完整有效的属性列表。

当你对一个类设置属性时,它的实例也会受到影响:

>>> f =Foo()>>>f.ham

Traceback (most recent call last):

File"", line 1, in AttributeError:'Foo' object has no attribute 'ham'

>>> Foo.ham = 'eggs'

>>>f.ham'eggs'

这是因为'ham'属性被添加到了Foo类的__dict__属性中:

>>> Foo.__dict__dict_proxy({'__module__': '__main__', 'bar': 'spam', 'ham': 'eggs', '__dict__': , '__weakref__': , '__doc__': None})>>> f.__dict__{}

尽管Foo的实例f自己的__dict__为空,但它还是能拥有作为类属性的'ham'。Python中,一个对象的属性查找顺序遵循首先查找实例对象自己,然后是类,接着是类的父类。

所以当你直接对一个实例设置属性时,会看到实例的__dict__中添加了该属性,但它所属的类的__dict__却不受影响。

>>> f.stack = 'overflow'

>>> f.__dict__{'stack': 'overflow'}>>> Foo.__dict__dict_proxy({'__module__': '__main__', 'bar': 'spam', 'ham': 'eggs', '__dict__': , '__weakref__': , '__doc__': None})

dir()所做的不是查找一个对象的__dict__属性(这个属性有时甚至都不存在),它使用的是对象的继承关系来反馈一个对象的完整的有效属性。

一个实例的__dict__属性仅仅是那个实例的局部属性集合,不包含该实例所有有效属性。所以如果你想获取一个对象所有有效属性,你应该使用dir()来替代__dict__或者__slots__。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值