在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()的区别的更多相关文章

  1. python中__dict__和dir()

    转自:http://www.cnblogs.com/ifantastic/p/3768415.html 首先需要知道的是,dir() 是 Python 提供的一个 API 函数,dir() 函数会自动 ...

  2. python中的__dict__和dir()的区别

    Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...

  3. python之__dict__与dir(转载)

    Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...

  4. Python 由__dict__和dir()引发的一些思考

    关于__dict__和dir()的区别和作用请参考这篇文章:http://blog.csdn.net/lis_12/article/details/53521554 说下我当时遇到的问题: class ...

  5. Python中type与Object的区别

    Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...

  6. Python中生成器和迭代器的区别(代码在Python3.5下测试):

    https://blog.csdn.net/u014745194/article/details/70176117 Python中生成器和迭代器的区别(代码在Python3.5下测试):Num01–& ...

  7. Python中的is和==的区别,==判断值是否相等,is判断地址是否一致

    Python中的is和==的区别 Python中的对象包含三要素:id.type.value. 其中id用来唯一标示一个对象,type标识对象的类型,value是对象的值. is判断的是a对象是否就是 ...

  8. 基于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 ...

  9. Python中send()和sendall()的区别

    Python中send()和sendall()的区别 估计每个学习Python网络编程的人,都会遇到过这样的问题: send()和sendall()到底有什么区别? send()和sendall()原 ...

随机推荐

  1. Linux内核设计与实现第四周读书笔记

    第5章系统调用 5.1与内核通信 主要作用: 为用户控件提供了一种硬件的抽象接口. 保证了系统稳定性与安全性. 为用户空间&系统提供公共接口. 5.2API.POSIX和C库 一般情况,应用程 ...

  2. [CTSC2007]动物园zoo

    link 试题分析 发现每个小朋友最多只能看到$5$个动物所以考虑状压$dp$.我们定义$f(i,j)$为第$i$个位置从此往后$5$个人的最喜欢数量.所以只要预处理出对于每个点从后$5$个会让多少小 ...

  3. 【bzoj3589】动态树

    Portal --> bzoj3589 Description 给你一棵\(n\)个节点的树,总共有\(q\)次操作,每次操作是以下两种中的一种: 操作\((0,x,delta)\):给以\(x ...

  4. Update submitted Perforce changelist description by P4.net api

    Firstly download the p4.net sdk from Perforce official site's download page. It's a .zip file, extra ...

  5. Codeforces Round #419 (Div. 2) A B C 暴力 区间更新技巧 +前缀和 模拟

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  6. laravel 5.1 Eloquent常见问题

    1.新增一条记录以及判断是否新增成功 $instance = XxxModel::create(['a' => 1, 'b' => 2]); if ($instance->exist ...

  7. python自学笔记(一)

    我没学过python,通过网上和一些图书资料,自学并且记下笔记. 很多细节留作以后自己做项目时再研究,这样能更高效一些. python基础自学笔记 一.基本输入和输出 pthon3.0用input提示 ...

  8. REST式的web服务

    “REST”是罗伊·菲尔丁(Roy Fielding)在他的博士论文中创造的缩写.菲尔丁论文的第5章勾画出了被称为REST风格或REST式的Web服务的知道原则.他是HTTP1.1规范的主要作者和Ap ...

  9. ③ 设计模式的艺术-03.工厂方法(Factory Method)模式

    public interface Car { void run(); } public class Audi implements Car { @Override public void run() ...

  10. 关于NuGet

    一.NuGet是什么? NuGet是Microsoft开发平台的程序集包管理器,它由客户端工具和服务端站点组成,客户端工具提供给用户管理和安装/卸载软件程序包,以及打包和发布程序包到NuGet服务端站 ...