使用 super() 和 super(ViewName,self) 之间的区别



我一直在通用视图(CBV(中使用

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)

但我注意到这里的人们确实:

context = super(ClassViewName,self).get_context_data(**kwargs)

有区别吗?

区别在于 python 版本支持的语法。在 python 3 中,您将使用

context = super().get_context_data(**kwargs)

而在 Python 2 中,你会使用

context = super(ClassViewName,self).get_context_data(**kwargs)

对于任何super方法调用都是如此

参见:http://www.pythonforbeginners.com/super/working-python-super-function

最新更新