1

I wonder what's a difference, is there any cause (apart from educational reasons) to use super() instead of self in this specific case:

class Pet():
    def hello(self): print("Hello, I am a Pet")


class Cat(Pet):
    def say(self):
        super().hello()

I'd use rather self if I wrote the say function's body: self.hello().
Any advice or opinions would be appreciated.

petezurich
  • 9,280
  • 9
  • 43
  • 57
maciejwww
  • 1,067
  • 1
  • 13
  • 26
  • 3
    The only reason to do this would be if you have a `hello` method in the subclass and you want to bypass it and go directly to the superclass. – Barmar Jun 21 '22 at 18:50
  • 1
    `super()` refers to to the parent (`Pet`), whereas `self` refers to your object (`Cat`). In _this_, specific case, `self.hello()` should work. But in other cases, like when a method is overridden in a child class, then you may need `super()`. – gen_Eric Jun 21 '22 at 18:51
  • 1
    Normally `super()` is only used when calling the same method from the parent. The most common example is when the subclass `__init__()` method needs to call the parent's `__init__()` method. – Barmar Jun 21 '22 at 18:51
  • Possible duplicate of [What is the purpose of the word 'self'?](https://stackoverflow.com/q/2709821/11082165) and/or [What does 'super' do in Python?](https://stackoverflow.com/q/222877/11082165) – Brian61354270 Jun 21 '22 at 18:51
  • Does this answer your question? [What's the difference between super().method() and self.method()](https://stackoverflow.com/questions/50658942/whats-the-difference-between-super-method-and-self-method) – Brian61354270 Jun 21 '22 at 18:53
  • It seems like you understand what `super()` does and what `self` is, and it seems like you have confirmed that both work fine in this circumstance. The rest is largely a matter of opinion; I don't think there's a suitable question for Stack Overflow remaining here. – Karl Knechtel Jun 21 '22 at 18:55
  • @KarlKnechtel Is there a better place for such questions in *Stack Exchange Network*? – maciejwww Jun 22 '22 at 02:13

3 Answers3

1

super() is designed for helping with inheritance, and is usually used in the same method as the one it's calling.

def hello(self):
    super().hello()

It's relatively rare to use super() to reference a different method.

def say(self):
    super().hello()  # say what?

It's legal. After all, super() just references the superclass's method, so if you really want to avoid calling self.hello() then this is one way you can do it.

But it's unusual and would probably make other coders wonder why you did it that way.

kojiro
  • 74,557
  • 19
  • 143
  • 201
1

To understand the difference between the two, you need to understand inheritance and how the interpreter handles class methods.

self is a reference to the object instance for which you are currently performing the application.

super allows you to access attributes (methods, members, etc) of an ancestor type. It is a reference to a class type, rather than a reference to an object instance.

When you call super().<something> you are telling the interpreter to look for a code definition in one of the parent classes. This way you can call the constructor of your parent class for example or override an implementation from a parent class and add some logic to it.

When you use self correctly (as in, you use that for the first argument in methods), this allows you to access other properties (methods, members, etc) for the instance you are currently handling.

The reason you use super when writing the __init__ function, is that without it, whenever you call any method on self, the interpreter will invoke any implementation defined for the top-most class type first. To tell the interpreter to skip it, you have to use super().

Daniel Trugman
  • 8,186
  • 20
  • 41
1

super() is a reference to the inherited class, whereas self is reference to the class itself.

class Pet():
    def hello(self): print("Hello, I am a Pet")


class Cat(Pet):
    def hello_there(self): print("Hello there! I am a Pet")

    def say(self):
        super().hello()
        super().hello_there()
    
    
mycat = Cat()
mycat.say()

This results in an error because the inherited class does not have the new method "hello_there()" which only exists in the new class we made.

enter image description here

But when you inherit from a class you also inherit from it's parents methods so this would work the same as the above example:

class Pet():
    def hello(self): print("Hello, I am a Pet")


class Cat(Pet):
    def hello_there(self): print("Hello there! I am a Pet")

    def say(self):
        self.hello()
        super().hello_there()
    
    
mycat = Cat()
mycat.say()

enter image description here

but if you wanted to use the new method "hello_there()" you need to reference it from self since it only exists in the child class.

class Pet():
    def hello(self): print("Hello, I am a Pet")


class Cat(Pet):
    def hello_there(self): print("Hello there! I am a Pet")

    def say(self):
        self.hello()
        self.hello_there()
    
    
mycat = Cat()
mycat.say()

enter image description here

Andew
  • 321
  • 1
  • 9