First of all, this question might be duplicate, but somehow I haven't got my head around those answers. Say I have:
import argparse
class Parent:
def __init__(self):
self.parser = argparse.ArgumentParser()
self.parser.add_argument("user")
# parse the argument
self.parser.parse_args()
class Child(Parent):
def __init__(self):
# here self.parser is not defined
# self.parser.add_argument("password")
super().__init__()
# here args are already parsed and
# self.parser does not include args
# self.parser.add_argument("password")
if __name__ == '__main__':
Child()
This is the minimal working example of the code I want to write (something like command line app framework. My setup is basically the following
class Parent:
def __init__(self):
# do some stuff starting stuff
# middle
# do some ending stuff
class Child(Parent):
def __init__(self):
# do something in the middle of Parent init
There is simple partial workaround in my case. When Parent
might not do self.parser.parse_args()
or it would somehow (don't know how write now exactly) check, if child added any arguments and then leave the self.parser.parse_args()
to the child class. But I thought, whether it is possible one dumb idea was to change parent's __init__
to behave like a decorator over child and do something like this (a part from the fact, that init does allow to return anything other than None
, it probably does not work from various other reasons..?)
class Child(Parent):
@super.__init__
def __init__(self):
So, any ideas, which path should I chose? My motivation for that is, that I want the end user, who would use such framework (that means writing app, that inherits from Parent) to need to do as little work as possible (that means, moving most things into Parent). Is there any reasonable way, how to do this?