Python __dict__与dir()

  • Python __dict__与dir

    • __dict__属性
    • dir函数
    • 结论

转载请标明出处(http://blog.csdn.net/lis_12/article/details/53521554).

Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。

__dict__与dir()的区别:

  1. dir()是一个函数,返回的是list;
  2. __dict__是一个字典,键为属性名,值为属性值;
  3. dir()用来寻找一个对象的所有属性,包括__dict__中的属性,__dict__是dir()的子集;

并不是所有对象都拥有__dict__属性。许多内建类型就没有__dict__属性,如list,此时就需要用dir()来列出对象的所有属性。

__dict__属性

__dict__是用来存储对象属性的一个字典,其键为属性名,值为属性的值。

#!/usr/bin/python
# -*- coding: utf-8 -*-
class A(object):class_var = 1def __init__(self):self.name = 'xy'self.age = 2@propertydef num(self):return self.age + 10def fun(self):passdef static_f():passdef class_f(cls):passif __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 = 3a.fun = lambda :xprint a.__dict__  #{'level1': 3, 'age': 2, 'name': 'xy','fun': <function <lambda> at 0x>}print A.__dict__  #与上述结果相同A.level2 = 4print 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>, 等.....'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

从上述代码可知,

  1. 实例的__dict__仅存储与该实例相关的实例属性,

    正是因为实例的__dict__属性,每个实例的实例属性才会互不影响。

  2. 类的__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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

结论

dir()函数会自动寻找一个对象的所有属性,包括__dict__中的属性。

__dict__是dir()的子集,dir()包含__dict__中的属性。

参考网址

  1. https://docs.python.org/2/howto/descriptor.html?highlight=descriptor%20protocol#id1
  2. http://stackoverflow.com/questions/4877290/what-is-the-dict-dict-attribute-of-a-python-class
  3. http://www.tuicool.com/articles/ZbQFF3u
  4. http://www.jb51.net/article/54540.htm
  5. http://blog.csdn.net/lis_12/article/details/53519060

python __dict__ ,dir()相关推荐

  1. 19 Python __dict__与dir()区别

    Python __dict__与dir() Python __dict__与dir __dict__属性 dir函数 结论 转载请标明出处(http://blog.csdn.net/lis_12/ar ...

  2. Python之dir()与__dict__的区别

    Python之dir()与__dict__的区别 - iFantasticMe 原文  http://www.cnblogs.com/ifantastic/p/3768415.html 主题 Pyth ...

  3. Python中dir,hasattr,getattr,setattr,vars的使用

    Python中dir,hasattr,getattr,setattr,vars的使用 Python一切皆对象,对象都有很多属性和方法,使用时我们怎么知道对象有哪些属性,以及如何获取对象的属性和设置对象 ...

  4. python里dir是什么意思_python中dir什么作用

    python中dir的作用是:1.dir函数传入数据类型返回该数据类型的所有内置方法:2.dir函数传入模块名返回该模块的所有属性和方法. dir() 内置函数的作用 python 内置方法有很多,无 ...

  5. python中dir的使用_python中dir是什么意思

    详细内容 python中dir是什么意思? python中dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__() ...

  6. python的dir()和__dict__属性的区别

    只要是有属性的数据对象(不一定是面向对象的对象实例,而是指具有数据类型的数据对象),都可以通过- ---- __dict__和dir()来显示数据对象的相关属性. __ dict__可以看作是数据对象 ...

  7. python中dir用法_Python内置函数dir详解

    1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: >>> help(dir) Help on built-in function dir in mo ...

  8. Python __dict__属性详解

    由此可见, 类的静态函数.类函数.普通函数.全局变量以及一些内置的属性都是放在类__dict__里的 对象的__dict__中存储了一些属性 我们都知道Python一切皆对象,那么Python究竟是怎 ...

  9. python 使用dir() help() 查看一个对象所有拥有的方法和属性

    可以使用python 的内置方法 dir() 或者help() 查看 某个对象所拥有的方法和属性, 二者间的区别是: dir() : 只是得到方法或者属性的名称 help():不但可以得到对象的方法和 ...

最新文章

  1. 请务必注意 Redis 安全配置,否则将导致轻松被入侵
  2. 判断是否在数组中,若在输入其下标,否则输入-1
  3. HDU4631Sad Love Story
  4. asterisk使用MYSQL认证的配置方法
  5. 华为鸿蒙电脑操作系统测试版,华为鸿蒙测试版下载 华为鸿蒙测试版电脑版下载...
  6. 漫步线性代数一——引言
  7. sql递归查询上级_递归的实际业务场景之MySQL 递归查询
  8. 链表的基本操作Basic Operation of LinkList
  9. 每天学点Linux:一
  10. 自定义ArcView-构造拓展性高的view
  11. wms开发语言c 还是java,专业WMS和普通WMS之间差异有什么呢?
  12. 计算机中堆栈指针的作用,堆栈指针是什么_有什么作用
  13. wps2016向程序发送命令_解决excel弹出“向程序发送命令时出现问题”的方法
  14. jquery fadeOut 异步
  15. Pr动态图形模板Mogrt导入失败 Mogrt is Corrupt 解决方法 Motion Graphics Templates is corrupt.
  16. spread 超链接跳转sheet 不触发 GC.Spread.Sheets.Events.ActiveSheetChanged 事件处理
  17. linux中安装中文拼音输入法过程
  18. 机器学习小组知识点14:均匀分布(Uniform Distribution)
  19. 记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
  20. 早安心语优美的心情语录

热门文章

  1. 好玩有趣的软件,居家无聊必备
  2. 关于 print 和 printf:
  3. Linux 重定向与管道
  4. 新闻文本分类 TextCNN
  5. python生成静态html_python – 从XML内容生成静态HTML站点
  6. 爬取场库网站遇到的问题
  7. 大数据开发工程师考试分享
  8. office 2010 中使用 mathtype6.8
  9. NET Framework 4.0
  10. python--生词本