简体   繁体   English

super()。method()和self.method()有什么区别

[英]What's the difference between super().method() and self.method()

What's the difference between using super().method() and self.method() , when we inherit something from a parent class and why use one instead of another? 当我们从父类继承某些东西时,为什么使用super().method()self.method()什么区别,为什么要使用一个而不是另一个呢?

The only thing that comes to my mind is that with static methods it becomes obviously impossible to call self.method() . 我唯一想到的是,使用静态方法显然无法调用self.method() As for everything else I can't come up with justification to use super() . 至于其他一切,我无法提出使用super()理由。

Could someone present a dummy example when choosing one over another matters and explain why, or is it just convention thing? 当有人选择另一个问题时,有人可以提出一个虚拟的例子,并解释原因,还是仅仅是常规的事情?

super().method() will call the parent classes implementation of method , even if the child has defined their own. 即使孩子定义了自己的methodsuper().method()也会调用super().method()的父类实现。 You can read the documentation for super for a more in-depth explanation. 您可以阅读super文档以获得更深入的解释。

class Parent:
    def foo(self):
        print("Parent implementation")

class Child(Parent):
    def foo(self):
        print("Child implementation")
    def parent(self):
        super().foo()
    def child(self):
        self.foo()

c = Child()
c.parent()
# Parent implementation
c.child()
# Child implementation

For singular-inheritance classes like Child , super().foo() is the same as the more explicit Parent.foo(self) . 对于像Child这样的单继承类, super().foo()与更明确的Parent.foo(self) In cases of multiple inheritance, super will determine which foo definition to use based on the Method Resolution Order, or MRO . 在多重继承的情况下, super将根据“ 方法解析顺序”或MRO确定要使用的foo定义。

A further motivating example: which method gets called if we subclass Child and write another implementation of foo ? 另一个有启发性的示例:如果我们将Child子类化并编写foo另一个实现,则调用哪个方法?

class Grandchild(Child):
    def foo(self):
        print("Grandchild implementation")

g = Grandchild()
g.parent()
# Parent implementation
g.child()
# Grandchild implementation

self

self , which is mostly used as the first parameter of instance methods of classes, always represents the calling object/instance of the class. self ,通常用作类的实例方法的第一个参数,它始终表示类的调用对象/实例。

super() 超()

super() refers to object of parent class. super()引用父类的对象。 It is useful in case of method overriding and this is in case of numerous programming languages including C++, Java etc. In Java, super() is used to call the constructor of parent class. 在方法重写和多语言(包括C ++,Java等)编程的情况下,此方法很有用。在Java中, super()用于调用父类的构造函数。

Please have a look at the below little example. 请看下面的小例子。

class TopClass(object):
    def __init__(self, name, age):
        self.name = name;
        self.age = age;

    def print_details(self):
        print("Details:-")
        print("Name: ", self.name)
        print("Age: ", self.age)
        self.method()

    def method(self):
        print("Inside method of TopClass")

class BottomClass(TopClass):
    def method(self):
        print("Inside method of BottomClass")    

    def self_caller(self):
         self.method()

    def super_caller(self):
         parent = super()
         print(parent)
         parent.method()

child = BottomClass ("Ryan Holding", 26)
child.print_details()

"""
Details:-
Name:  Ryan Holding
Age:  26
Inside method of BottomClass
"""

parent = TopClass("Rishikesh Agrawani", 26)
parent.print_details()

"""
Details:-
Name:  Rishikesh Agrawani
Age:  26
Inside method of TopClass
"""

child.self_caller()
child.super_caller()

"""
Inside method of BottomClass
<super: <class 'BottomClass'>, <BottomClass object>>
Inside method of TopClass
"""

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

相关问题 Python-self.method,lambda之间的区别:启动时self.method()和self.method() - Python - difference between self.method, lambda: self.method() and self.method() on startup 使用 super() 和使用 self 从父类调用方法有什么区别? - What is the difference between calling a method from parent class using super() and using self? 在方法的开头或结尾调用super()有什么区别? - What is the difference between super() being called at the beginning or end of a method? 如何在类创建时使用self.method()? - How to use self.method() in class creation time? __repr__方法中的self和object参数有什么区别? - What is the difference between self and object parameters in __repr__ method? 属性和类方法有什么区别? - what's the difference between property and class method? Django:重写create()方法和save方法()有什么区别? - Django: What's the difference between overriding the create() method and the save method()? Python 3中的“函数”,“方法”和“绑定方法”之间有什么区别? - What's the difference between a 'function', 'method' and 'bound method' in Python 3? Python中的字符串方法和str方法有什么区别? - what's the difference between string method and str method in Python? 为什么我们更喜欢使用self.method()而不是Class.method(self) - Why do we prefer to use self.method() instead of Class.method(self)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM