If there's a class
Class A:
def __init__(arg1, kwarg1=...):
...something happens here with args and kwargs
is there a way to add another argument by monkey patching this class? How to keep everything that happens in it's __init__
in place, without repeating it anywhere?
this worked:
from package import module
class ChangedInitClass(module.SomeClass):
def __init__(self, arg1, another_arg, kwarg1=...):
super().__init__(arg1, kwarg1=kwarg1)
# does something with another_arg
module.SomeClass = ChangedInitClass
It affects also classes that are subclasses of A:
old_init = A.__init__
def new_init(self, foo, *args, *kwargs):
old_init(self, *args, *kwargs)
self.foo = foo
A.__init__ = new_init
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With