如何获得Python类的父母?

问题:如何获得Python类的父母?

如何获得Python类的父类?

How can I get the parent class(es) of a Python class?


回答 0

使用以下属性:

cls.__bases__

文档

类对象的基类的元组。

例:

>>> str.__bases__
(<type 'basestring'>,)

另一个例子:

>>> class A(object):
...   pass
... 
>>> class B(object):
...   pass
... 
>>> class C(A, B):
...   pass
... 
>>> C.__bases__
(<class '__main__.A'>, <class '__main__.B'>)

Use the following attribute:

cls.__bases__

From the docs:

The tuple of base classes of a class object.

Example:

>>> str.__bases__
(<type 'basestring'>,)

Another example:

>>> class A(object):
...   pass
... 
>>> class B(object):
...   pass
... 
>>> class C(A, B):
...   pass
... 
>>> C.__bases__
(<class '__main__.A'>, <class '__main__.B'>)

回答 1

如果要所有祖先而不是直接祖先,请使用inspect.getmro

import inspect
print inspect.getmro(cls)

有用的是,这为您提供了“方法解析顺序”中的所有祖先类-即在解析方法(或实际上是任何其他属性时,将检查祖先的顺序)-方法和其他属性都位于同一个命名空间中毕竟是在Python中;-)。

If you want all the ancestors rather than just the immediate ones, use inspect.getmro:

import inspect
print inspect.getmro(cls)

Usefully, this gives you all ancestor classes in the “method resolution order” — i.e. the order in which the ancestors will be checked when resolving a method (or, actually, any other attribute — methods and other attributes live in the same namespace in Python, after all;-).


回答 2

新型类具有可调用的mro方法,该方法按方法解析顺序返回父类列表。

New-style classes have an mro method you can call which returns a list of parent classes in method resolution order.


回答 3

最快的方式查看所有父母,然后按顺序使用内置__mro__

repr(YOUR_CLASS.__mro__)


>>>
>>>
>>> import getpass
>>> getpass.GetPassWarning.__mro__

输出,IN ORDER


(<class 'getpass.GetPassWarning'>, <type 'exceptions.UserWarning'>,
<type 'exceptions.Warning'>, <type 'exceptions.Exception'>, 
<type 'exceptions.BaseException'>, <type 'object'>)
>>>

你有它。目前“最佳”的答案是182票(我在输入时),但这比一些复杂的for循环要简单得多,一次查看一个类,更不用说当一个类扩展两个或两个以上父级时类。导入和使用inspect仅会不必要地覆盖范围。老实说,人们不知道仅使用内置功能是一种耻辱

我希望这有帮助!

The FASTEST way, to see all parents, and IN ORDER, just use the built in __mro__

i.e. repr(YOUR_CLASS.__mro__)


>>>
>>>
>>> import getpass
>>> getpass.GetPassWarning.__mro__

outputs, IN ORDER


(<class 'getpass.GetPassWarning'>, <type 'exceptions.UserWarning'>,
<type 'exceptions.Warning'>, <type 'exceptions.Exception'>, 
<type 'exceptions.BaseException'>, <type 'object'>)
>>>

There you have it. The “best” answer right now, has 182 votes (as I am typing this) but this is SO much simpler than some convoluted for loop, looking into bases one class at a time, not to mention when a class extends TWO or more parent classes. Importing and using inspect just clouds the scope unnecessarily. It honestly is a shame people don’t know to just use the built-ins

I Hope this Helps!


回答 4

如果您只想获取父母,请使用基数,使用__mro__(如@ naught101所指出的)获取方法解析顺序(以便知道初始化的执行顺序)。

基础(首先获取现有对象的类):

>>> some_object = "some_text"
>>> some_object.__class__.__bases__
(object,)

对于最新Python版本中的mro:

>>> some_object = "some_text"
>>> some_object.__class__.__mro__
(str, object)

显然,当您已经有了一个类定义时,您可以直接调用__mro__它:

>>> class A(): pass
>>> A.__mro__
(__main__.A, object)

Use bases if you just want to get the parents, use __mro__ (as pointed out by @naught101) for getting the method resolution order (so to know in which order the init’s were executed).

Bases (and first getting the class for an existing object):

>>> some_object = "some_text"
>>> some_object.__class__.__bases__
(object,)

For mro in recent Python versions:

>>> some_object = "some_text"
>>> some_object.__class__.__mro__
(str, object)

Obviously, when you already have a class definition, you can just call __mro__ on that directly:

>>> class A(): pass
>>> A.__mro__
(__main__.A, object)

回答 5

如果要确保它们都被调用,请super在所有级别上使用。

If you want to ensure they all get called, use super at all levels.