My class hierarchy is set up so that each child __init__()must set self._init_has_run()to False, call the parent __init__(), then make its own __init__()and finally set self._init_has_run()to True. I have the following code:
class Parent:
def __init__(self, arg1, arg2):
pass
def init(cls, fun):
def decorated_init(self, *args, **kwargs):
self._init_has_run = False
x = super()
super().__init__(*args, **kwargs)
fun(self, *args, **kwargs)
self._init_has_run = True
return decorated_init
class Child(Parent):
@Parent.init
def __init__(self, arg1, arg2):
pass
Since there are a number of subclasses that follow the same general pattern for __init__(), and I can't figure out how to use metaclasses, I use a decorator to consolidate repeating logic, and then just apply this decorator to all stream methods __init__().
Python throws the following:
File "filename.py", line 82, in decorated_init
super().__init__(*args, **kwargs)
TypeError: object.__init__() takes no parameters
, self._init_has_run super() , super().__init__(*args, **kwargs), Python object.__init__() ?