Skip to main content
 首页 » 编程设计

python之python 如何使用 dir(object) 显示所有属性和方法的列表

2024年12月31日9insus

我在阅读 python dunder 方法时发现我们可以对像 len(object) 这样的内置方法有自己的定义
所以我创建了一个类 otherclass ,它有定义

>>> class otherclass: 
        def __len__(self): 
            return 0 
 
>>> len(otherclass()) 
0 
 
>>> dir(otherclass) 
['__doc__', '__len__', '__module__'] 

它确实打印了对象的 len 并进一步检查我还发现 len dunder 方法在使用 dir 的其他类对象中可用

我想知道 python 如何显示 dir(object) 的值,因为 dir(otherclass) 的结果中没有 dir dunder 方法

请您参考如下方法:

我的意思是我 (python 3.6) dir(otherclass) 打印

['__class__', 
 '__delattr__', 
 '__dict__', 
 '__dir__', 
 '__doc__', 
 '__eq__', 
 '__format__', 
 '__ge__', 
 '__getattribute__', 
 '__gt__', 
 '__hash__', 
 '__init__', 
 '__init_subclass__', 
 '__le__', 
 '__len__', 
 '__lt__', 
 '__module__', 
 '__ne__', 
 '__new__', 
 '__reduce__', 
 '__reduce_ex__', 
 '__repr__', 
 '__setattr__', 
 '__sizeof__', 
 '__str__', 
 '__subclasshook__', 
 '__weakref__'] 

最有趣的是 __dict__。

otherclass.__dict__ == \ 
mappingproxy({'__dict__': <attribute '__dict__' of 'otherclass' objects>, 
              '__doc__': None, 
              '__len__': <function __main__.otherclass.__len__>, 
              '__module__': '__main__', 
              '__weakref__': <attribute '__weakref__' of 'otherclass' objects>}) 

所以这包含所有特定于该对象的方法/属性。然而,所有其他人从何而来?好吧,来自父类(super class):

object.__dict__

object.__dict__ == \  
mappingproxy({'__class__': <attribute '__class__' of 'object' objects>, 
              '__delattr__': <slot wrapper '__delattr__' of 'object' objects>, 
              '__dir__': <method '__dir__' of 'object' objects>, 
              '__doc__': 'The most base type', 
              '__eq__': <slot wrapper '__eq__' of 'object' objects>, 
              '__format__': <method '__format__' of 'object' objects>, 
              '__ge__': <slot wrapper '__ge__' of 'object' objects>, 
              '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, 
              '__gt__': <slot wrapper '__gt__' of 'object' objects>, 
              '__hash__': <slot wrapper '__hash__' of 'object' objects>, 
              '__init__': <slot wrapper '__init__' of 'object' objects>, 
              '__init_subclass__': <method '__init_subclass__' of 'object' objects>, 
              '__le__': <slot wrapper '__le__' of 'object' objects>, 
              '__lt__': <slot wrapper '__lt__' of 'object' objects>, 
              '__ne__': <slot wrapper '__ne__' of 'object' objects>, 
              '__new__': <function object.__new__>, 
              '__reduce__': <method '__reduce__' of 'object' objects>, 
              '__reduce_ex__': <method '__reduce_ex__' of 'object' objects>, 
              '__repr__': <slot wrapper '__repr__' of 'object' objects>, 
              '__setattr__': <slot wrapper '__setattr__' of 'object' objects>, 
              '__sizeof__': <method '__sizeof__' of 'object' objects>, 
              '__str__': <slot wrapper '__str__' of 'object' objects>, 
              '__subclasshook__': <method '__subclasshook__' of 'object' objects>}) 

所以dir寻找__dir__方法,然后寻找__dict__(或__slots__),然后递归依次在每个类中查找方法解析顺序(可用 otherclass.mro())。