[Tutor] Using super() in classes

Manprit Singh manpritsinghece at gmail.com
Fri Nov 26 11:06:08 EST 2021


Dear sir,

Consider the below given examples :

class ABC:
    def show(self):
        print("HELLO")

class ABCD(ABC):
    def show(self):
        print("HI")
        super().show()

obj = ABCD()
obj.show()
will give output as :

HI
HELLO

Which is my desired output, here i have used super() inside class ABCD, so

that I can call show() of class ABC inside ABCD. Here If I write self.show()

instead of super().show(), it will call show() of class ABCD and it will

proceed like a recursive function .

Just need to know if my explanation is correct or not . Secondarily
the above given

given example can be written in the below written way or not ? Here instead

of writing super().show() in class ABCD i have written ABC.show(self) .According

to me it will serve the same purpose as that of the above written classes.

Am I correct ?


class ABC:
    def show(self):
        print("HELLO")

class ABCD(ABC):
    def show(self):
        print("HI")
        ABC.show(self)

obj = ABCD()
obj.show()

My second Example where i am calling show() of parent class inside a method of

child class whose name is not show(), but child class has a method with

name show, You can see i have called show() of parent class by writing

super().show() inside the greeting method of child class . It it ok to
do like this ?


class ABC:
    def show(self):
        print("HELLO")

class ABCD(ABC):
    def show(self):
        print("HI")

    def greeting(self, Name):
        super().show()
        print(Name)

obj = ABCD()
obj.greeting("Ravi")

Which gives the desired output as below:

HELLO
Ravi

Kindly guidr


More information about the Tutor mailing list