Python super(Class,self).method vs super(Parent,self).method [英] Python super(Class, self).method vs super(Parent, self).method

查看:186
本文介绍了Python super(Class,self).method vs super(Parent,self).method的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自以下问题,让我们说 B类 extends A类

This question is derive from the following question, let's say class B extends class A

class A(object):
  def do_work(self):
    print 123

class B(A):
  def do_work(self):
    super(B,self).do_work() # versus the next statement
    super(A,self).do_work() # what's the difference?


推荐答案

super(B,self).do_work()

将调用 do_work B 的父类看到的函数 - 即 A.do_work

will call the do_work function as seen by the parent class of B - that is, A.do_work.

super(A,self).do_work()

将调用 do_work 函数,如<$ c $的父类所示c> A - 即 object.do_work (可能不存在,因此可能引发异常)。

will call the do_work function as seen by the parent class of A - that is, object.do_work (which probably doesn't exist, and thus would likely raise an exception).

这篇关于Python super(Class,self).method vs super(Parent,self).method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆