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()原 ...
随机推荐
- bzoj3709: [PA2014]Bohater(贪心)
贪心... 可以回血的按d[i]升序防止死掉 不能回血的按a[i]降序,因为只考虑d我们要扣除的血量是一定的,为了不死显然回血多的放前面更好 #include<iostream> #inc ...
- 公告:开通csdn博客,敬请关注!
公告:开通csdn博客,敬请关注!地址:https://blog.csdn.net/cyjch
- Java中将对象转换为Map的方法
将对象转换为Map的方法,代码如下: /** * 将对象转成TreeMap,属性名为key,属性值为value * @param object 对象 * @return * @throws Illeg ...
- SpringMVC 重定向
在返回视图名字的字符串前面加forword:或redirect:前缀是就会对他们做特殊处理,它们分别是转发和重定向 我们测试一个重定向操作把 Java代码 @RequestMapping(" ...
- bzoj 1588 平衡树 splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 15446 Solved: 6076[Submit][Sta ...
- 省选模拟赛 arg
1 arg (arg.cpp/in/out, 1s, 512MB)1.1 Description给出一个长度为 m 的序列 A, 请你求出有多少种 1...n 的排列, 满足 A 是它的一个 LIS. ...
- mysql主从同步碰到的问题
一.mysql 安装https://www.cnblogs.com/jxrichar/p/9248480.html二.主从配置参考https://www.cnblogs.com/superfat/p/ ...
- [Luogu 1967] NOIP2013 货车运输
[Luogu 1967] NOIP2013 货车运输 一年多前令我十分头大的老题终于可以随手切掉了- 然而我这码风又变毒瘤了,我也很绝望. 看着一年前不带类不加空格不空行的清纯码风啊,时光也好像回去了 ...
- Codeforces Round #191 (Div. 2) B. Hungry Sequence(素数筛选法)
. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 爬取知乎话题async使用协程
import requests import json import time from pyquery import PyQuery import pandas as pd from collect ...