class Rectangle: def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width class Square(Rectangle): def __init__(self, length): # Call the __init__ method of the parent class (Rectangle) # super() refers to the parent class of this class # Actually using the __init__ method of `Rectangle` here # which needs 2 parameters, `length` and `width`. Since # a square is length==width then we use `length` twice. super().__init__(length, length) my_square = Square(5) my_square.area() # 25
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter