zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    CF453A
    各种算法的复杂度
    状压dp--P2622 关灯问题II
    笛卡尔树模板
    20201122模拟
    nm树上背包+二分--P4322 [JSOI2016]最佳团体
    20201121模拟
    20201119模拟
    斜率优化--P3195 [HNOI2008]玩具装箱
    网络流--最大流,最小割,费用流问题
  • 原文地址:https://www.cnblogs.com/tlz888/p/10175039.html
Copyright © 2011-2022 走看看