How to extend Python class init How to extend Python class init python python

How to extend Python class init


You can just define __init__ in the subclass and call super to call the parents' __init__ methods appropriately:

class SubThing(Thing):    def __init__(self, *args, **kwargs):        super(SubThing, self).__init__(*args, **kwargs)        self.time = datetime.now()

Make sure to have your base class subclass from object though, as super won't work with old-style classes:

class Thing(object):    ...


You should write another __init__ method in SubThing and then call the constructor of the superclass to initialize its fields.

This Q&A should provide you some more examples.