python中__dict__与dir()的区别
在python中__dict__与dir()都可以返回一个对象的属性,区别在于:
- __dict__是对象的一个属性,而dir()是一个built-in的方法;
- __dict__返回一个对象的属性名和值,即dict类型,而dir()返回一个属性名的list;
- __dict__不是每一个对象都有,而dir()对各种类型对象都起作用,如module, type, class, object;
- _dict__只返回对象的可写属性(writable attributes),而dir()返回各种相关(relavent)属性,且对于不同类型对象,作用不同。
2.7版官方文档描述如下:
__dict__
object.__dict__
A dictionary or other mapping object used to store an object’s (writable) attributes.
dir()
The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
- If the object is a module object, the list contains the names of the module’s attributes.
- If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
- Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.
python中__dict__与dir()的区别的更多相关文章
- python中__dict__和dir()
转自:http://www.cnblogs.com/ifantastic/p/3768415.html 首先需要知道的是,dir() 是 Python 提供的一个 API 函数,dir() 函数会自动 ...
- python中的__dict__和dir()的区别
Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...
- python之__dict__与dir(转载)
Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...
- Python 由__dict__和dir()引发的一些思考
关于__dict__和dir()的区别和作用请参考这篇文章:http://blog.csdn.net/lis_12/article/details/53521554 说下我当时遇到的问题: class ...
- 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()原 ...
随机推荐
- day2-python基础
- POJ--2752
原题链接:http://poj.org/problem?id=2752 分析:no! #include<cstdio> #include<cstring> #include&l ...
- 题解【luogu3709 大爷的字符串题】
Description 个人觉得这是这道题最难的一步...出题人的语文... 每次给出一个区间,求这个区间最少能被多少个单调上升的序列覆盖. Solution 这个东西可以转化为这个区间中出现次数最多 ...
- 手脱PEtite v2.1
1.载入PEID PEtite v2.1 2.载入OD,先F8跟一下 0042C10F > B8 00C04200 mov eax,跑跑排行.0042C000 ; //程序入口点 0042C11 ...
- JS函数大全 莫名其妙找到的
1 .document.write(""); 输出语句 2 .JS中的注释为// 3 .传统的HTML文档顺序是:document->html->(head,body) ...
- JS零碎小知识
filter()方法对数组进行过滤,生成新数组 var aqiNewData = aqiData.filter(function(data){ return data[1]>60; }); // ...
- Linux查找后执行命令
find . -name '*.jsp' -type f -print -exec rm -rf {} \; 在当前目录下找到jsp文件并删除.
- 1.ideal常用快捷键
Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...
- 重构改善既有代码设计--重构手法11:Move Field (搬移字段)
你的程序中,某个字段被其所驻类之外的另一个类更多的用到.在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段. 动机:在类之间移动状态和行为,是重构过程中必不可少的措施.随着系 ...
- mongoDB与sql聚合操作对应图
SQL Terms, Functions, and Concepts MongoDB Aggregation Operators WHERE $match GROUP BY $group HAVING ...