wan*_*nik 6 python super method-resolution-order
我想知道从 super() 函数获得的实例的类型。我试着print(super())和__print(type(super()))__
class Base:
def __init__(self):
pass
class Derive(Base):
def __init__(self):
print(super())
print(type(super()))
super().__init__()
d = Derive()
Run Code Online (Sandbox Code Playgroud)
结果是
<super: <class 'Derive'>, <Derive object>>
<class 'super'>
Run Code Online (Sandbox Code Playgroud)
有了这些结果,我想知道如何super().__init__()调用正确的构造函数。
根据您的评论,您想知道如何super知道接下来要调用哪个方法。Super 检查实例的 mro,知道它所在的当前类方法,并调用下一个方法。以下演示将在 Python 2 和 3 中运行,在 Python 2 中,由于元类,它会打印每个类的名称,因此我将使用该输出:
首先进行导入和设置以使打印更好:
import inspect
class Meta(type):
def __repr__(cls):
return cls.__name__
Run Code Online (Sandbox Code Playgroud)
接下来,我们定义一个函数来根据超级对象本身告诉我们发生了什么
def next_in_line(supobj):
print('The instance class: {}'.format(supobj.__self_class__))
print('in this class\'s method: {}'.format(supobj.__thisclass__))
mro = inspect.getmro(supobj.__self_class__)
nextindex = mro.index(supobj.__thisclass__) + 1
print('super will go to {} next'.format(mro[nextindex]))
Run Code Online (Sandbox Code Playgroud)
最后,我们根据维基百科C3 线性化条目中的示例声明一个类层次结构,对于一个足够复杂的示例,请注意元类repr在 Python3 中不起作用,但属性分配不会破坏它。super(Name, self)另请注意,我们使用与 Python 3 中等效的完整 super 调用super(),并且仍然有效:
class O(object):
__metaclass__ = Meta
def __init__(self):
next_in_line(super(O, self))
super(O, self).__init__()
class A(O):
def __init__(self):
next_in_line(super(A, self))
super(A, self).__init__()
class B(O):
def __init__(self):
next_in_line(super(B, self))
super(B, self).__init__()
class C(O):
def __init__(self):
next_in_line(super(C, self))
super(C, self).__init__()
class D(O):
def __init__(self):
next_in_line(super(D, self))
super(D, self).__init__()
class E(O):
def __init__(self):
next_in_line(super(E, self))
super(E, self).__init__()
class K1(A, B, C):
def __init__(self):
next_in_line(super(K1, self))
super(K1, self).__init__()
class K2(D, B, E):
def __init__(self):
next_in_line(super(K2, self))
super(K2, self).__init__()
class K3(D, A):
def __init__(self):
next_in_line(super(K3, self))
super(K3, self).__init__()
class Z(K1, K2, K3):
def __init__(self):
next_in_line(super(Z, self))
super(Z, self).__init__()
Run Code Online (Sandbox Code Playgroud)
现在,当我们打印 Z 的 mro 时,我们得到了应用于继承树的该算法定义的方法解析顺序:
>>> print(inspect.getmro(Z))
(Z, K1, K2, K3, D, A, B, C, E, O, <type 'object'>)
Run Code Online (Sandbox Code Playgroud)
当我们调用 Z() 时,因为我们的函数使用 mro,所以我们将按顺序访问每个方法:
>>> Z()
The instance class: Z
in this class's method: Z
super will go to K1 next
The instance class: Z
in this class's method: K1
super will go to K2 next
The instance class: Z
in this class's method: K2
super will go to K3 next
The instance class: Z
in this class's method: K3
super will go to D next
The instance class: Z
in this class's method: D
super will go to A next
The instance class: Z
in this class's method: A
super will go to B next
The instance class: Z
in this class's method: B
super will go to C next
The instance class: Z
in this class's method: C
super will go to E next
The instance class: Z
in this class's method: E
super will go to O next
The instance class: Z
in this class's method: O
super will go to <type 'object'> next
Run Code Online (Sandbox Code Playgroud)
我们停在object.__init__。从上面我们可以看到,super总是知道实例所在的类,当前所在类的方法,并且可以从实例类的 MRO 中推断出下一步要去哪里。
我想知道基类的名称?
如果您只需要直接基数(或多个基数,在多重继承的情况下),则可以使用该__bases__属性,该属性返回一个元组
>>> Derive.__bases__
(<class __main__.Base at 0xffeb517c>,)
>>> Derive.__bases__[0].__name__
'Base'
Run Code Online (Sandbox Code Playgroud)
我建议使用检查模块来获取方法解析顺序(super基于原始调用者的类):
>>> import inspect
>>> inspect.getmro(Derive)
(<class __main__.Derive at 0xffeb51dc>, <class __main__.Base at 0xffeb517c>)
Run Code Online (Sandbox Code Playgroud)
从超级获取
super().__self_class__给出实例类,并super().__thisclass__给出当前类。我们可以使用实例的 MRO 并查找接下来的类。我想您不会在最终父级中执行此操作,因此我没有捕获索引错误:
class Base:
def __init__(self):
print(super().__self_class__)
print(super().__thisclass__)
class Derive(Base):
def __init__(self):
print(super().__self_class__)
print(super().__thisclass__)
mro = inspect.getmro(super().__self_class__)
nextindex = mro.index(super().__thisclass__) + 1
print('super will go to {} next'.format(mro[nextindex]))
super().__init__()
>>> d = Derive()
<class '__main__.Derive'>
<class '__main__.Derive'>
super will go to <class '__main__.Base'> next
<class '__main__.Derive'>
<class '__main__.Base'>
Run Code Online (Sandbox Code Playgroud)