Python class Inheritance(Doubt)

When we are over-riding the init() of Rectangle ,then why can’t we put both length,height arguments .Try putting both the arguments once.It returns error

class Rectangle():

    def __init__(self,length,height):
        self.length = length
        self.height = height

    def area(self):
        return self.length * self.height

    def __repr__(self):

rect_obj = Rectangle(3,4)


class Square(Rectangle):

    def __init__(self,length,height):
        super(Square,self).__init__(self,length) #When we are over-riding the __init__() of Rectangle ,then why can't we put both length,height arguments .
        self.length = length
        self.height = height

square_obj = Square(2,4)



You said it right there in your question

This means that when you have a square, the init for the rectangle has been replaced and is no longer valid. Otherwise, you could make a ‘square’ with mismatched height and length, which shouldn’t be possible.

Sorry,I didn’t get this concept,can you explain in detail.

I see what’s going on now.

Your rectangle class has this __init__ method.

But to create a rectangle you call

Notice that in Python, methods internal to an object have an extra argument, self. But when you call those methods outside of the object, like above, you don’t include this argument.

Now lets look at your attempt to make a square class

When calling the rectangle class constructor, you only need to call with two arguments, length and height. You should not be calling super(Square,self).__init__ with the self argument.

As a note, your constructor for the square should only have a single argument, as the length == height on a square.

Clearly explained,thanks