python中查询对象的属性可以用dir()和__dict__,区别在于:
dir()是python提供的API函数,利用对象的继承关系来查询该对象的所有有效属性,查询顺序为从对象本身向上级查询,下级的属性查询不到。
__dict__本身作为对象的一种属性,查询范围区别于dir()利用继承关系查询,__dict__仅限于对象本身属性。但是并不是所有对象都有__dict__属性:
a>如果类的属性中有__slots__属性(关于__slots__属性,本文不作详解),则该类的实例没有__dict__属性;
b>pythopn许多内建类型都没有__dict__属性,比如列表list
因此__dict__只有对有该属性的对象有效。
便于理解对比,上代码:
#python 2.7
class B(object):
b = 'bbbbb'
class A(B):
a = 'aaaaa'
A_instance = A()
print 'A.__dict__:'
print A.__dict__
print '-'*50
print 'A_instance.__dict__:'
print A_instance.__dict__
print '-'*50
print 'dir(A):'
print dir(A)
print '-'*50
print 'dir(A_instance):'
print dir(A_instance)
print '-'*50
A.c = 'ccccc'
print '给类A添加属性c后:'
print 'A.__dict__:'
print A.__dict__
print '-'*50
print 'A_instance.__dict__:'
print A_instance.__dict__
print '-'*50
print 'dir(A):'
print dir(A)
print '-'*50
print 'dir(A_instance):'
print dir(A_instance)
print '-'*50
A_instance.d = 'ddddd'
print '给类A实例A_instance添加属性d后:'
print 'A.__dict__:'
print A.__dict__
print '-'*50
print 'A_instance.__dict__:'
print A_instance.__dict__
print '-'*50
print 'dir(A):'
print dir(A)
print '-'*50
print 'dir(A_instance):'
print dir(A_instance)
print '-'*50
输出:
A.__dict__:
{'a': 'aaaaa', '__module__': '__main__', '__doc__': None}
--------------------------------------------------
A_instance.__dict__:
{}
--------------------------------------------------
dir(A):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
--------------------------------------------------
dir(A_instance):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b']
--------------------------------------------------
给类A添加属性c后:
A.__dict__:
{'a': 'aaaaa', 'c': 'ccccc', '__module__': '__main__', '__doc__': None}
--------------------------------------------------
A_instance.__dict__:
{}
--------------------------------------------------
dir(A):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c']
--------------------------------------------------
dir(A_instance):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c']
--------------------------------------------------
给类A实例A_instance添加属性d后:
A.__dict__:
{'a': 'aaaaa', 'c': 'ccccc', '__module__': '__main__', '__doc__': None}
--------------------------------------------------
A_instance.__dict__:
{'d': 'ddddd'}
--------------------------------------------------
dir(A):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c']
--------------------------------------------------
dir(A_instance):
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'b', 'c', 'd']
--------------------------------------------------
关于dir()、__dict__和__slots__的区别,也可以参考http://www.cnblogs.com/ifantastic/p/3768415.html?utm_source=tuicool,里面讲的很详细