您的位置:首页 > 编程语言 > Python开发

Python之dir()与__dict__的区别

2015-07-22 17:52 831 查看


Python之dir()与__dict__的区别 - iFantasticMe

原文 http://www.cnblogs.com/ifantastic/p/3768415.html
主题 Python

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

不是所有的对象都有__dict__属性。例如,如果你在一个类中添加了__slots__属性,那么这个类的实例将不会拥有__dict__属性,但是dir()仍然可以找到并列出它的实例所有有效属性。
>>> class Foo(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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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__:
>>> class Foo(object):
...     bar = 'spam'
... 
>>> Foo().__dict__
{}
>>> Foo.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__module__': '__main__', 'bar': 'spam', '__doc__': None})


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

当你对一个类设置属性时,它的实例也会受到影响:
>>> f = Foo()
>>> f.ham
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__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__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None})


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

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

关于__slots__

合理使用__slots__属性可以节省一个对象所消耗的空间。一个普通对象使用一个dict来保存它自己的属性,你可以动态地向其中添加或删除属性,但是如果使用__slots__属性,那么该对象用来保存其自身属性的结构一旦创建则不能再进行任何修改。因此使用slot结构的对象节省了一部分开销。虽然有时这是一个很有用的优化方案,但是它也可能没那么有用,因为如果Python解释器足够动态,那么它完全可以在向对象添加属性时只请求该对象所使用的dict。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: