Ketan Patel
Ketan Patel

Reputation: 69

When to use self, super() and class name while calling super method from child?

super().method() and ClassName.method() do the same thing but when to use super().method() vs self.method()?

Based on my understanding, one will use super().method() when super method is called from the same method whereas will use self.method() when calling from other methods of the child class.

class Animal():
    def run(self):
        print('running')

    def walk(self):
        print('walking')


class Cat(Animal):
    def run(self):
        super().run()

    def walk_fast(self):
        self.walk()       ---> super().walk() also works but any reason using one over the other ?

c = Cat()
c.run()
c.walk_fast()

Upvotes: 1

Views: 621

Answers (4)

Bill
Bill

Reputation: 540

Actually the name explained everything, if you write the code this way you can see the difference

class Animal:
    def run(self):
        print('running')

    def walk(self):
        print('walking')


class Cat(Animal):
    def run(self):
        # override to make a difference between self.run() and super().run()
        print('running like a cat')

    def run_super(self):
        super().run()

    def run_self(self):
        self.run()


c = Cat()
c.run() # running like a cat
c.run_super() #running
c.run_self() # running like a cat
c.walk() # Cat don't have walk, call go to Animal (e.g. super())

Upvotes: 0

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

The code below should make it clear for you

class Animal():
    def run(self):
        print('running')

    def walk(self):
        print('Animal is walking')

    #Static method called walk
    @staticmethod
    def static_walk():
        print('Animal is walking in static')

class Cat(Animal):

    def run(self):
        super().run()

    def walk(self):
        print('Cat is walking')

    def walk_fast(self):
        #This calls walk of Animal
        super().walk()

        #This calls walk of Cat
        self.walk()

        #This calls static method walk of Animal
        Animal.static_walk()

The output will be

running
Animal is walking
Cat is walking
Animal is walking in static
  • Cat().walk_fast(), super().walk() is going to call the walk of Animal class, hence you see Animal is walking.

  • Doing super().method() will call the method of the superclass, if it is implemented in the superclass,

  • self.walk() is going to call the walk of Cat class, hence you see Cat is walking

  • self.method() calls the method of the class itself

  • Doing ClassMethod.method() will call the static method of the class, so super().method() and ClassName.method() are not the same!

  • Animal.static_walk() is going to call the static method of Animal class, hence you see Animal is walking in static

Upvotes: 0

Joe6.626070
Joe6.626070

Reputation: 319

Using super() is a reference to the parent class. It is usually used with descriptors and magic methods such as __init__. It allows you to call a method directly from the parent class without having to define the parent classes name. It also allows you to move multiple inheritance levels following the mro

There is no direct difference from using self except when there is a conflict with the name of the method i.e

class Animal():
    def run(self):
        print('running')

    def walk_fast(self):
        print('walking')


class Cat(Animal):
    def run(self):
        super().run()

    def walk_fast(self):
        super(Cat).walk_fast()
        print('After walking')

c = Cat()
c.run()
c.walk_fast()

Upvotes: 0

gkhnavarro
gkhnavarro

Reputation: 466

super() references the parent class while self.method() executes the method of the class itself.

Since Cat inherits from Animal, c.run() should print running.

However, you do not have to redefine the run() function in Cat because it already inherits the methods from Animal. c.run() will already print running.

Similarly, self.walk() is working your function because it is defined in Animal, and Cat inherits from Animal.

super() is usually used with __init__(), where you want to initialize the parent class's properties in the child class. Take a look at this question for more details.

Upvotes: 0

Related Questions