achedeuzot
achedeuzot

Reputation: 4384

Is there a purpose in calling super() from a basic python class?

I have somewhat of a strange question here. Let's say I'm making a simple, basic class as follows:

class MyClass(object):

    def __init__(self):
        super(MyClass, self).__init__()

Is there any purpose in calling super()? My class only has the default object parent class. The reason why I'm asking this is because my IDE automagically gives me this snippet when I create a new class. I usually remove the super() function because I don't see any purpose in leaving it. But maybe I'm missing something ?

Upvotes: 0

Views: 43

Answers (1)

Steve Jessop
Steve Jessop

Reputation: 279255

You're not obliged to call object.__init__ (via super or otherwise). It does nothing.

However, the purpose of writing that snippet in that way in an __init__ function (or any function that calls the superclass) is to give you some flexibility to change the superclass without modifying that code.

So it doesn't buy you much, but it does buy you the ability to change the superclass of MyClass to a different class whose __init__ likewise accepts no-args, but which perhaps does do something and does need to be called by subclass __init__ functions. All without modifying your MyClass.__init__.

Your call whether that's worth having.

Also in this particular example you can leave out MyClass.__init__ entirely, because yours does nothing too.

Upvotes: 1

Related Questions