【问题标题】:Python super(Class, self).method vs super(Parent, self).methodPython super(Class, self).method vs super(Parent, self).method
【发布时间】:2013-02-07 04:51:18
【问题描述】:

这个问题源自以下question,假设class B 扩展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?

【问题讨论】:

    标签: python inheritance parent super


    【解决方案1】:
    super(B,self).do_work()
    

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


    super(A,self).do_work()
    

    将调用A 的父类所看到的do_work 函数,即object.do_work(它可能不存在,因此可能会引发异常)。

    【讨论】:

    猜你喜欢
    • 2013-11-15
    • 2021-07-18
    • 2012-08-03
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多