pythondir什么意思_Python之dir()与__dict__的区别
首先需要知道的是,dir()是Python提供的一個(gè)API函數(shù),dir()函數(shù)會(huì)自動(dòng)尋找一個(gè)對(duì)象的所有屬性,包括搜索__dict__中列出的屬性。
不是所有的對(duì)象都有__dict__屬性。例如,如果你在一個(gè)類中添加了__slots__屬性,那么這個(gè)類的實(shí)例將不會(huì)擁有__dict__屬性,但是dir()仍然可以找到并列出它的實(shí)例所有有效屬性。
>>> 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']
同理許多內(nèi)建類型都沒有__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()函數(shù)和類實(shí)例
Python的實(shí)例擁有它們自己的__dict__,而它們對(duì)應(yīng)的類也有自己的__dict__:
>>> classFoo(object):
... bar= 'spam'...>>> Foo().__dict__{}>>> Foo.__dict__dict_proxy({'__dict__': , '__weakref__': , '__module__': '__main__', 'bar': 'spam', '__doc__': None})
dir()函數(shù)會(huì)遍歷Foo,Foo()和object的__dict__屬性,從而為Foo類,它的實(shí)例以及它所有的被繼承類創(chuàng)建一個(gè)完整有效的屬性列表。
當(dāng)你對(duì)一個(gè)類設(shè)置屬性時(shí),它的實(shí)例也會(huì)受到影響:
>>> 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'
這是因?yàn)?#39;ham'屬性被添加到了Foo類的__dict__屬性中:
>>> Foo.__dict__dict_proxy({'__module__': '__main__', 'bar': 'spam', 'ham': 'eggs', '__dict__': , '__weakref__': , '__doc__': None})>>> f.__dict__{}
盡管Foo的實(shí)例f自己的__dict__為空,但它還是能擁有作為類屬性的'ham'。Python中,一個(gè)對(duì)象的屬性查找順序遵循首先查找實(shí)例對(duì)象自己,然后是類,接著是類的父類。
所以當(dāng)你直接對(duì)一個(gè)實(shí)例設(shè)置屬性時(shí),會(huì)看到實(shí)例的__dict__中添加了該屬性,但它所屬的類的__dict__卻不受影響。
>>> f.stack = 'overflow'
>>> f.__dict__{'stack': 'overflow'}>>> Foo.__dict__dict_proxy({'__module__': '__main__', 'bar': 'spam', 'ham': 'eggs', '__dict__': , '__weakref__': , '__doc__': None})
dir()所做的不是查找一個(gè)對(duì)象的__dict__屬性(這個(gè)屬性有時(shí)甚至都不存在),它使用的是對(duì)象的繼承關(guān)系來反饋一個(gè)對(duì)象的完整的有效屬性。
一個(gè)實(shí)例的__dict__屬性僅僅是那個(gè)實(shí)例的局部屬性集合,不包含該實(shí)例所有有效屬性。所以如果你想獲取一個(gè)對(duì)象所有有效屬性,你應(yīng)該使用dir()來替代__dict__或者_(dá)_slots__。
總結(jié)
以上是生活随笔為你收集整理的pythondir什么意思_Python之dir()与__dict__的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: optfine的jar文件打不开_JAV
 - 下一篇: 锅炉低氮燃烧器原理(低氮燃烧器工作原理)