python中的dir()和__dict__

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,里面讲的很详细

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值