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()原 ...
随机推荐
- linux安全第二周学习总结
一.实验过程 cd LinuxKernel/linux-3.9.4 qemu -kernel arch/x86/boot/bzImage 然后cd mykernel 您可以看到qemu窗口输出的内容的 ...
- 51nod 1225 数学
F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数. 例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + ...
- SGU179 Brackets light
179. Brackets light time limit per test: 0.25 sec. memory limit per test: 131072 KB input: standard ...
- Linux系统之路——用CentOS 7打造合适的科研环境
安装CentOS CentOS 7的安装与其他Linux发行版的安装差不多,个别地方稍有不同. 准备工作 准备材料 U盘:容量700M以上,用于制作U盘启动盘,因为在制作启动盘时会格式化U盘,所以U盘 ...
- flask学习之解决Internal Server Error问题的方式之一
最近在学习flask web development的时候,遇到了这么一个问题,就是照着书上敲的代码,跑起来是Internal server error,由于中途学的时候为了方便,改用pycharm来 ...
- 011. C++ friend使用
1.friend 友元 将一个函数定义为friend,可以读取private数据: 显然,friend提供便利的同时,会破坏C++的封装性,因此,建议谨慎使用,朋友多了也许是个困扰. class co ...
- .Net平台下实例类型无法转换成接口类型?
首先这种情况出现在应用程序启动前的方法里面. 本想通过发射来实现一些功能.谁知道被这个坑了. 碰到这种问题.已经相当无语了.同时也不知道该如何解决.望有能之士帮忙解答 using System; us ...
- Android Studio Gradle's dependency cache may be corrupt Re-download dependencies and sync project (requires network)
转:Android studio 快速解决Gradle's dependency cache may be corrupt 和 Gradle配置 gradle-3.*-all.zip快速下载 Andr ...
- spring boot 2.0.3+spring cloud (Finchley)7、服务链路追踪Spring Cloud Sleuth
参考:Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] Spring Cloud Sleuth 是Spring Cloud的一个组件,主要功能是 ...
- Codeforces 807 A Is it rated?
http://codeforces.com/problemset/problem/807/A A. Is it rated? time limit per test 2 se ...