如何找到Python对象的隐藏属性?(未出现在dir(obj)列表中的属性)

vla*_*ean 5 python reflection

我正在使用IPython.

我声明了一个简单的类MyClass(object),并且在控制台中,当使用名称MyClass时,在dot运算符之后,我点击了Tab.

我得到的第一个建议之一是mro,意思MyClass.mro.我点击输入,我得到的输出是:

> <function mro>
Run Code Online (Sandbox Code Playgroud)

现在我没有定义这个方法,当然它返回我的类的方法解析顺序.

此方法不会出现在返回的列表中dir(MyClass),因此我的问题出现了:

如何找到类或其他对象的任何其他隐藏功能?

Bak*_*riu 4

dir()并不试图提供完整的输出,只是提供在交互式解释器中有用的合理近似值。

输出中不包含 and 的原因可以在源代码dir__mro__第1812行找到mroObjects/object.c

/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
   We deliberately don't suck up its __class__, as methods belonging to the
   metaclass would probably be more confusing than helpful.
*/
static PyObject *
_specialized_dir_type(PyObject *obj)
Run Code Online (Sandbox Code Playgroud)

事实证明,mro方法和__mro__属性实际上是元类的属性,即type

>>> '__mro__' in object.__dict__
False
>>> '__mro__' in type.__dict__
True
>>> 'mro' in object.__dict__
False
>>> 'mro' in type.__dict__
True
Run Code Online (Sandbox Code Playgroud)

因此它们不会显示在 的输出中dir。这是否明智取决于。我相信大多数时候您确实不想看到元类定义的内容,因为它们是元类的内部结构。


正如dir()文档中所解释的,您可以自定义其输出定义__dir__方法。然而,这适用于该类的实例:

class A(object):
  def __dir__(self):
    return ['a', 'b', 'c']

print(dir(A()), dir(A))
Run Code Online (Sandbox Code Playgroud)

输出:

(['a', 'b', 'c'], ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'])
Run Code Online (Sandbox Code Playgroud)

如果您还想自定义输出,则dir(A)必须使用自定义元类:

class MyMeta(type):
  def __dir__(self):
    return ['a', 'b', 'c']


class A(object, metaclass=MyMeta):
  # __metaclass__ = MyMeta   # in python 2

  def __dir__(self):
    return ['a', 'b', 'c', 'd']


print(dir(A()), dir(A))
Run Code Online (Sandbox Code Playgroud)

输出:

(['a', 'b', 'c', 'd'], ['a', 'b', 'c'])
Run Code Online (Sandbox Code Playgroud)