[Tutor] Calling super classs __init__?

John Fouhy john at fouhy.net
Wed Mar 19 01:37:53 CET 2008

On 19/03/2008, Allen Fowler <allen.fowler at yahoo.com> wrote:
>  I have a super class that accepts many arguments to it's constructor, and a subclass that should define one additional argument.
>  What's the most "pythonic" way to make this work?

class BaseClass(object):
  def __init__(self, x, y, z, foo='foo'): # whatever
     # etc

class SubClass(BaseClass):
  def __init__(self, t, *args, **kw):
    BaseClass.__init__(self, *args, **kw)
    # do something with t

This does mean that the special sub class argument has to come before
the base class arguments when you create instances.

Whether you call BaseClass.__init__ early or late in the subclass init
method could depend on what your classes are doing.  Remember, in
Python, __init__ only initializes objects, it doesn't create them.
It's just another bit of code that you can call whenever you want.


More information about the Tutor mailing list