I wanted an inheritance chain to have an optional parameter. Most classes along the chain need to have the parameter become a member sometimes, but I also want to use the chain other times without the parameter becoming a member.
I thought of making the parameter optional a class and using import, but I want to avoid using class syntax for the member optional
. And also because all classes along the chain are used in a dictionary as keys.
Alternatives to this? Am I doing something wrong? Is there a more Pythonic way?
class Top:
def __init__(self, optional=None):
if optional is not None:
self.optional = optional
return
class Middle(Top):
def __init__(self, one, optional=None):
if optional is not None:
super().__init__(optional)
self.one = one
class Bottom(Middle):
def __init__(self, one, two, optional=None):
if optional is not None:
super().__init__(one, optional)
else:
super().__init__(one)
self.two = two
a = Middle('one')
b = Middle('one', 'two')
c = Bottom('one', 'two')
d = Bottom('one', 'two', 'three')