Python 从 super 或从 class 调用方法有区别吗

Python is there a difference calling a method from super, or from the class

使用 Django 我习惯于使用 super 覆盖通用视图中的方法:

class MyClass(SomeGenericView):

    def method_to_override(self, request, *args, **kwargs):
        # do something extra here  

        return super(MyClass, self).method_to_override(request, *args, **kwargs)

我注意到 pydev 自动完成从父 class 调用方法:

class MyClass(SomeGenericView):

    def method_to_override(self, request, *args, **kwargs):
        # do something extra here  

        return SomeGenericView.method_to_override(self, request, *args, **kwargs)

这些方法之间有什么区别吗?是否出于某种原因偏爱其中之一?

如果我没记错的话,第二个例子会导致无限循环,因为它是一个没有任何停止条件的递归调用。

我认为它应该替换为 parent class 的名称,但即便如此,当您使用 class MyClass 时也会有一些差异。

在第一个例子中,子class会调用MyClass中的方法,也就是parent中的方法,而在第二个中,它会调用[=12]中的方法=],因为它是硬编码的。

除非你处理的是旧式 classes(这里不是这种情况),否则使用 super() 是 RightThing(tm) 的做法,因为 super()将根据继承图正确调用正确的 "next" 方法——请记住,Python 确实支持多重继承(FWIW 在 Django 的基于 class 的视图中广泛使用)。