In Python 3.x with the given class hierarchy, where SubClassZero
and SubClassOne
inherit from SuperClass
;
class SuperClass(object):
def __init__(self, *args, **kwargs):
self.thing_zero = "Inherited string."
class SubClassZero(SuperClass):
def __init__(self, *args, **kwargs):
SuperClass.__init__(self, *args, **kwargs)
self.thing_one = "SubClassZero's string."
class SubClassOne(SuperClass):
def __init__(self, *args, **kwargs):
SuperClass.__init__(self, *args, **kwargs)
self.thing_one = "SubClassOne's string."
scz = SubClassZero()
sco = SubClassOne()
If we want to give the subclasses the ability set a title from a kwarg
then the __init__
function of SuperClass can be redefined using the the built setattr
function like so;
def __init__(self, *args, **kwargs):
self.thing_zero = "Inherited string."
if 'title' in kwargs:
self.title = kwargs['title']
else:
self.title = ""
setattr(SuperClass, '__init__', __init__)
But to do that one has to know the previous structure of __init__
to maintain full functionality. Is there a way to somehow add to the __init__
function of SuperClass
without completely overwriting it?