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()

If you’re still on Python 2.x, make sure the base class is a subclass of object, as super won’t work with old-style classes:

class Thing(object):
    ...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)