【问题标题】:difference between using super() and super(ViewName,self) [duplicate]使用 super() 和 super(ViewName,self) 之间的区别 [重复]
【发布时间】:2018-04-08 00:21:29
【问题描述】:

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

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

但我注意到这里的人这样做:

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

有区别吗?

【问题讨论】:

    标签: python django django-views django-class-based-views django-context


    【解决方案1】:

    区别在于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

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 2013-11-15
      • 2019-02-20
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2019-04-26
      相关资源
      最近更新 更多