1

I was reading some Python code in a private repository on GitHub and found a class resembling the one below:

class Point(object):
    '''Models a point in 2D space. '''

    def __init__(self, x, y):
        super(Point, self).__init__()
        self.x = x
        self.y = y

    # some methods

    def __repr__(self):
        return 'Point({}, {})'.format(self.x, self.y)

I do understand the importance and advantages of using the keyword super while initialising classes. Personally, I find the first statement in the __init__ to be redundant as all the Python classes inherit from object. So, I want to know what are the advantages(if any) of initializing a Point object using super when inheriting from the base object class in Python ?

Kshitij Saraogi
  • 6,821
  • 8
  • 41
  • 71
  • Some might claim it's safer for multiple inheritance, but slapping `super().__init__()` at the top of your `__init__` isn't nearly enough to ensure safe, correct behavior in a multiple inheritance situation; for example, the necessary arguments aren't likely to reach the `__init__` methods that need them. Multiple inheritance is *hard*. – user2357112 Jun 19 '17 at 18:42

1 Answers1

1

There is none in this particular case as object.__init__() is an empty method. However, if you were to inherit from something else, or if you were to use multiple inheritance, call to the super.__init__() would ensure that your classes get properly initialized (assuming of course they depend on initialization from their parent classes). Without that Python's MRO cannot do its magic, for example.

zwer
  • 24,943
  • 3
  • 48
  • 66