简体   繁体   English

Python super(Class,self).method vs super(Parent,self).method

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

This question is derive from the following question , let's say class B extends class A 这个问题来自以下问题 ,比方说class B class A扩展了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()

will call the do_work function as seen by the parent class of B - that is, A.do_work . 将调用do_work函数,如B的父类所见 - 即A.do_work


super(A,self).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). 将调用do_work函数,如A的父类所示 - 即object.do_work (可能不存在,因此可能引发异常)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用 super().method() 与 BaseClass.method(self) - Calling super().method() vs. BaseClass.method(self) 使用 super() 和使用 self 从父类调用方法有什么区别? - What is the difference between calling a method from parent class using super() and using self? Python - 如何使用 super(<class> , 自己)?</class> - Python - how to access instance properties in parent classes with super(<class>, self)? 从子级调用 super 方法时何时使用 self、super() 和类名? - When to use self, super() and class name while calling super method from child? super()。method()和self.method()有什么区别 - What's the difference between super().method() and self.method() 在Python 2.7中,通过super(self .__ class__,self)调用Super Constructor不是更好吗? - In Python 2.7, isn't it better to call the Super Constructor via super(self.__class__, self)…? super(Foo,self)和super(Foo,self .__ class__)之间的区别? - Difference between super(Foo, self) and super(Foo, self.__class__)? Python self和super在多重继承中 - Python self and super in multiple inheritance 在没有`self`的〜Factory子类中调用factory_boy超类方法。 - Call a factory_boy super class method in a ~Factory sub-class without `self` 类名与 super() 中的自调用之间的区别 - different between class name vs self calling in super()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM