python 中dir()和__dict__的区别
Python __dict__与dir()
出处(http://blog.csdn.net/lis_12/article/details/53521554).
Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。
__dict__与dir()的区别:
- dir()是一个函数,返回的是list;
__dict__是一个字典,键为属性名,值为属性值;- dir()用来寻找一个对象的所有属性,包括
__dict__中的属性,__dict__是dir()的子集;
并不是所有对象都拥有__dict__属性。许多内建类型就没有__dict__属性,如list,此时就需要用dir()来列出对象的所有属性。
__dict__属性
__dict__是用来存储对象属性的一个字典,其键为属性名,值为属性的值。
#!/usr/bin/python
# -*- coding: utf-8 -*-
class A(object):
class_var = 1
def __init__(self):
self.name = 'xy'
self.age = 2 @property
def num(self):
return self.age + 10 def fun(self):pass
def static_f():pass
def class_f(cls):pass if __name__ == '__main__':#主程序
a = A()
print a.__dict__ #{'age': 2, 'name': 'xy'} 实例中的__dict__属性
print A.__dict__
'''
类A的__dict__属性
{
'__dict__': <attribute '__dict__' of 'A' objects>, #这里如果想深究的话查看参考链接5
'__module__': '__main__', #所处模块
'num': <property object>, #特性对象
'class_f': <function class_f>, #类方法
'static_f': <function static_f>, #静态方法
'class_var': 1, 'fun': <function fun >, #类变量
'__weakref__': <attribute '__weakref__' of 'A' objects>,
'__doc__': None, #class说明字符串
'__init__': <function __init__ at 0x0000000003451AC8>}
''' a.level1 = 3
a.fun = lambda :x
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy','fun': <function <lambda> at 0x>}
print A.__dict__ #与上述结果相同 A.level2 = 4
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy'}
print A.__dict__ #增加了level2属性 print object.__dict__
'''
{'__setattr__': <slot wrapper '__setattr__' of 'object' objects>,
'__reduce_ex__': <method '__reduce_ex__' of 'object' objects>,
'__new__': <built-in method __new__ of type object at>,
等.....
'''
从上述代码可知,
实例的
__dict__仅存储与该实例相关的实例属性,正是因为实例的
__dict__属性,每个实例的实例属性才会互不影响。类的
__dict__存储所有实例共享的变量和函数(类属性,方法等),类的__dict__并不包含其父类的属性。
dir()函数
dir()是Python提供的一个API函数,dir()函数会自动寻找一个对象的所有属性(包括从父类中继承的属性)。
一个实例的__dict__属性仅仅是那个实例的实例属性的集合,并不包含该实例的所有有效属性。所以如果想获取一个对象所有有效属性,应使用dir()。
print dir(A)
'''
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'class_f', 'class_var', 'fun', 'level1', 'level2', 'name', 'num', 'static_f']
'''
a_dict = a.__dict__.keys()
A_dict = A.__dict__.keys()
object_dict = object.__dict__.keys()
print a_dict
print A_dict
print object_dict
'''
['fun', 'level1', 'age', 'name'] ['__module__', 'level2', 'num', 'static_f', '__dict__', '__weakref__', '__init__', 'class_f', 'class_var', 'fun', '__doc__'] ['__setattr__', '__reduce_ex__', '__new__', '__reduce__', '__str__', '__format__', '__getattribute__', '__class__', '__delattr__', '__subclasshook__', '__repr__', '__hash__', '__sizeof__', '__doc__', '__init__']
''' #因为每个类都有一个__doc__属性,所以需要去重,去重后然后比较
print set(dir(a)) == set(a_dict + A_dict + object_dict) #True
结论
dir()函数会自动寻找一个对象的所有属性,包括__dict__中的属性。
__dict__是dir()的子集,dir()包含__dict__中的属性。
参考网址
- https://docs.python.org/2/howto/descriptor.html?highlight=descriptor%20protocol#id1
- http://stackoverflow.com/questions/4877290/what-is-the-dict-dict-attribute-of-a-python-class
- http://www.tuicool.com/articles/ZbQFF3u
- http://www.jb51.net/article/54540.htm
- http://blog.csdn.net/lis_12/article/details/53519060
python 中dir()和__dict__的区别的更多相关文章
- python中dir(),__dict__
dir()是python的一个函数, dir()函数如果接受的参数是一个类,则返回这个类所有的类变量和方法 dir()函数如果接收的参数是一个类的实例,则返回这个实例所有的实例变量,对应的类的类变量, ...
- Python中type与Object的区别
Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...
- Python中生成器和迭代器的区别(代码在Python3.5下测试):
https://blog.csdn.net/u014745194/article/details/70176117 Python中生成器和迭代器的区别(代码在Python3.5下测试):Num01–& ...
- Python中的is和==的区别,==判断值是否相等,is判断地址是否一致
Python中的is和==的区别 Python中的对象包含三要素:id.type.value. 其中id用来唯一标示一个对象,type标识对象的类型,value是对象的值. is判断的是a对象是否就是 ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- Python中send()和sendall()的区别
Python中send()和sendall()的区别 估计每个学习Python网络编程的人,都会遇到过这样的问题: send()和sendall()到底有什么区别? send()和sendall()原 ...
- Python中的is和==的区别
Python中的is和==的区别 1. is 是比较内存地址id() a = "YongJie" b = "YongJie" print(id(a)) #233 ...
- python中_new_()与_init_()的区别
__new__方法的使用 只有继承于object的新式类才能有__new__方法,__new__方法在创建类实例对象时由Python解释器自动调用,一般不用自己定义,Python默认调用该类的直接父类 ...
- python中break和continue的区别
python中break和continue的区别 break和continue 1.break 意思为结束循环 例: i = 0 while i<10: i+=1 if ...
随机推荐
- 高并发TCP连接数目问题
linux可通过五元组唯一确定一个链接:源IP,源端口,目的IP,目的端口,传输层协议.而一个端口不允许被两个及以上进程占用(一个进程可同时占用多个端口),据此是否可以推测一台linux服务器最多可以 ...
- C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
转载于:http://blog.csdn.net/tjpuacm/article/details/26389441 不加比较函数的情况: int a[]={0,1,2,2,3}; printf(&qu ...
- webRTC源码下载 Windows Mac(iOS) Linux(Android)全
webRTC源码下载地址:https://pan.baidu.com/s/18CjClvAuz3B9oF33ngbJIw 提取码:wl1e Windows版:visual studio 2017工 ...
- MFC中的句柄
1.引出句柄 CDC问题:1.CDC dc;dc.LineTo(point);无法运行 2.CDC *dc=GetDC();dc->LineTo(point);就可以运行了 MFC中有大量的句柄 ...
- Sublime Text3工具的安装、破解、VIM功能vintage插件教程
1.安装Sublime Text 3 下载安装:http://www.sublimetext.com/3 Package Control安装:https://sublime.wbond.net/in ...
- Development Tools
Introduction Even Chris created his article of Useful Reference Books ages ago I just bumped into it ...
- hashMap put方法 第二行代码
if (table == EMPTY_TABLE) { inflateTable(threshold); } table transient Entry<K,V>[] table = (E ...
- Axiom3D:Buffer漫谈
在前面数据绑定基本流程,简单说了下,在Axiom中,数据从我们C#的托管环境到下面的OpenGL或是D3D的非托管环境,有个转化过程,相关实现我们可以从BufferBase看起.BufferBase与 ...
- 技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)
一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这 ...
- SAP 以工序为基准进行发料 机加工行业 Goods Issue to Routing
SAP 以工序为基准进行发料 这个流程是在业务有关需求,业务需要按照工序发料,一个工单有多个工序,而料是要发到每个工序上,而且没到工序之间在物理上是有距离的,所以仓管员在打印配发单之后希望了解到哪 ...