代码之家  ›  专栏  ›  技术社区  ›  Mohamed Benkedadra

使用super()和super(ViewName,self)之间的区别[重复]

  •  0
  • Mohamed Benkedadra  · 技术社区  · 8 年前

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

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

    但我注意到这里的人会:

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

    有区别吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   polo    8 年前

    不同之处在于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