在 Python 2.7 中,通过 super(self.__class__, self)... 调用超级构造函数不是更好吗?

In Python 2.7, isn't it better to call the Super Constructor via super(self.__class__, self)...?

在Python2.7中调用parentclass的构造函数,我看到的标准代码是:

super(Child, self).__init__(self, val),

其中 Child 是 child class。 (在 Python 3.x 中,这被简化了,但我现在必须使用 2.7。)我的问题是,"canonical" 在 [= 中使用 super 不是更好吗? 29=] 2.7 为:

super(self.__class__, self).__init__(self, val)?

我已经对此进行了测试,它似乎有效。有什么理由不使用这种方法吗?

当 class 被子classed 时,此构造导致 RecursionError

MCVE:

class A(object):
    def __init__(self):
        super(self.__class__, self).__init__()


class B(A):
    pass

B()

In Python 3.x, this is simplified, but I have to use 2.7 for now.

在 Python 2.7 中调用超级构造函数的更好方法是使用 Python-Future。它允许使用 Python 3 super() in Python 2 with:

from builtins import super  # from Python-Future

class Parent(object):
    def __init__(self):
        print('Hello')

class Child(Parent):
    def __init__(self):
        super().__init__()


Child()

输出:

Hello