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.

转载于:https://www.cnblogs.com/tlz888/p/10175039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值