获取所有可能 Class 属性的完整列表

Get complete list of all possible Class Attributes

有没有办法,给定一个简单的 Class,输出它所有可能的属性?标准属性如 __class____doc__ special read only attributes__mro__, __bases__等。一般全部呈现属性?

考虑 Class 最简单的情况:

class myClass:
    pass

dir()vars()inspect.getmembers() 都排除了某些 builtin 属性。最完整的列表是通过使用 myClass.__dir__(MyClass) 提供的,它在添加内置属性时排除了 MyClass 的用户定义属性,例如:

In [3]: set(MyClass.__dir__(MyClass)) - set(dir(MyClass))
Out[3]: 
{'__abstractmethods__', '__base__', '__bases__',
 '__basicsize__', '__call__', '__dictoffset__',
 '__flags__', '__instancecheck__', '__itemsize__',
 '__mro__', '__name__', '__prepare__', '__qualname__',
 '__subclasscheck__', '__subclasses__', '__text_signature__',
 '__weakrefoffset__', 'mro'}

根据添加的类似问题之一,这是不可能的。如果目前仍然不可能,"hiding" 某些 属性如 __bases__ 背后的基本原理是什么(从标准调用到 dir(), vars() & inspect 而不是像 __name__?


类似问题:

dir() 基本上是一种方便的方法,它不应该 return 一切,它基本上做的是它递归地 returns 在 return 的字典中找到的一切=58=]及其基础。

PyPy's implementation of dir() 很好理解:

def dir(*args):
    ...
    elif isinstance(obj, (types.TypeType, types.ClassType)):
        # Don't look at __class__, as metaclass methods would be confusing.
        return sorted(_classdir(obj))
    ...

def _classdir(klass):
    """Return a set of the accessible attributes of class/type klass.

    This includes all attributes of klass and all of the base classes
    recursively.
    """
    names = set()
    ns = getattr(klass, '__dict__', None)
    if ns is not None:
        names.update(ns)
    bases = getattr(klass, '__bases__', None)
    if bases is not None:
        # Note that since we are only interested in the keys, the order
        # we merge classes is unimportant
        for base in bases:
            names.update(_classdir(base))
    return names

由于每个 class 基本上都继承自 object,您会看到包含一些双下划线方法,因为它们实际上是 object 字典的一部分:

>>> class A(object):
    pass
...
>>> set(dir(A)) == set(list(object.__dict__) + list(A.__dict__))
True

Now what about __bases__ and other missing items?

首先object本身就是某物的一个实例,:

>>> isinstance(type, object)
True
>>> isinstance(object, type)
True
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False
>>> type.mro(object)
[<type 'object'>]
>>> type.mro(type)
[<type 'type'>, <type 'object'>]

因此,__bases____ge__ 等所有属性实际上都是 type 的一部分:

>>> list(type.__dict__)
['__module__', '__abstractmethods__', '__getattribute__', '__weakrefoffset__', '__dict__', '__lt__', '__init__', '__setattr__', '__subclasses__', '__new__', '__base__', '__mro__', 'mro', '__dictoffset__', '__call__', '__itemsize__', '__ne__', '__instancecheck__', '__subclasscheck__', '__gt__', '__name__', '__eq__', '__basicsize__', '__bases__', '__flags__', '__doc__', '__delattr__', '__le__', '__repr__', '__hash__', '__ge__']

因此,当我们执行 A.__bases__ 时,我们实际上是在查找 descriptor on type of A,即 type:

>>> A.__bases__
(<type 'object'>,)
>>> type(A).__dict__['__bases__'].__get__(A, type)
(<type 'object'>,)

因此,由于 Atype 的一个实例,这些方法不是它自己的字典的一部分,而是它的类型的字典。

>> class A(object):
...     spam = 'eggs'
...
>>> a = A()
>>> a.foo = 100
>>> a.bar = 200
>>> a.__dict__
{'foo': 100, 'bar': 200}
>>> A.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, 'spam': 'eggs'})

因为 typeobject 的子 class,type 上的 dir() 调用将包含来自 object 的一些项目:

>>> set(dir(type)) - set(type.__dict__)
set(['__reduce_ex__', '__str__', '__format__', '__reduce__', '__class__', '__subclasshook__', '__sizeof__'])