Home > Software design >  Can we use super method in a newly created class without any inheritance?
Can we use super method in a newly created class without any inheritance?

Time:09-26

I am new to classes in python. I searched and went through so many articles on super() method which creates so much of confusion.

Type - 1

class Square():
    def __init__(self,side):
        self.side = side
        print("Its confusing")
        super().__init__()
  
    def area(self):
        return self.side * self.side
    
c = Square()
print(c.area())

In Type - 1 , if I mention super().__init__() what is the use of it? I don't understand why we use super().__init__() in a newly created class which is not inherited from any other class. And I don't understand what it does here.

If we give it after assigning self.side = side it runs properly and is executed.

Type - 2

class Square():
    def __init__(self):
        # self.side = side
        # print("Its confusing")
        super().__init__()
e = Square()
e()

If I just give only super().__init__() inside __init__ it gives error. TypeError: 'Square' object is not callable

My doubt is:

1.Can we use super().__init__() in a newly created class? and It runs well without error.

2.Then why is Type - 2 throwing error if I put only super().__init__() inside __init__?

Can you please make it in simple words?

CodePudding user response:

For type-1 because Square class does not inherit from any base class, then the call for super().__init__() actually does nothing.

However, it's still good to keep this code, in case you use Square class for multiple inheritance. There is an example in a different question, see here: https://stackoverflow.com/a/8613067/362792. So copying that example, suppose you had class Square, class Color, and then class ColoredSquare:

class ColoredSquare(Square, Color):
   ...

In this case, when creating ColoredSquare instance, the super().__init__() from Square is actually calling Color.__init__() method. So it's indeed necessary then.


For type-2, your error is not from the init method. Instead, it's actually e() is producing your error for "TypeError: 'Square' object is not callable".

To use an object in this way you need to have call method. eg.

class Square():
    def __init__(self):
        super().__init__()

    def __call__(self):
        print('call for square')


e = Square()
e()

CodePudding user response:

Inheritance is used to recycle code... and you use super to "fetch" code defined in a parent class.

# parent class
class Parallelogram:
    def __init__(self, side1, side2):
        self.side1, self.side2 = side1, side2

    def area(self):
        return self.side1 * self.side2


# child class
class Square(Parallelogram):
    def __init__(self, side):
        super().__init__(side, side)


p = Parallelogram(2, 3)
print(p.area())
# 6

s = Square(4)
print(s.area())
# 16

print(issubclass(Parallelogram, object))
#True
print(issubclass(Square, Parallelogram))
#True

print(hasattr(Square, 'area')) # inherited from the parent 
True
  • Related