Reputation: 7619
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 ?
Upvotes: 1
Views: 69
Reputation: 25809
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.
Upvotes: 1