Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inheritance - super

We can also call super() passing a different class name

class Point():
    def __init__(self, x, y):
        print('__init__ of point')
        self.x = x
        self.y = y

class Circle(Point):
    def __init__(self, x, y, r):
        print('__init__ of circle')
        super().__init__(x, y)
        self.r = r

class Ball(Circle):
    def __init__(self, x, y, r, z):
        print('__init__ of ball')
        #super(Circle, self).__init__(x, y) # r
        Point.__init__(self, x, y) # r
        self.z = z


b = Ball(2, 3, 10, 7)
print(b)

# __init__ of ball
# __init__ of point
# <__main__.Ball object at 0x10a26f190>