Croll
Croll

Reputation: 3791

How to override base constructor arguments

If I make a python class which overrides another class, does __init__ get called from base class? What if I want to specify arguments for it?

Base class:

class bar(object):
    def __init__(self, somearg = False):
        # ...

New class

class foo(bar):
    def __init__(self)
       # ???

What do I want?

obj = foo() # somearg = True

Upvotes: 1

Views: 2632

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121834

No, the base class __init__ method is not called, since your derived class provides a new version of the method.

You'd call the parent __init__ method explicitly through the super() proxy and pass in the argument to set a different default:

class foo(bar):
    def __init__(self)
        super().__init__(somearg=True)

Upvotes: 4

Related Questions