Let's say I have some simple python function in a module
def f(x):
print(x)
This function just prints its argument. However, I want to modify this function or class method, depending on what it actually is.
I have a second function:
def g(x):
x.append(1)
which manipulates the input argument (probably a list) and appends some item. Now, I want to inject a call to g into f so that the latter would behave as
def f_patched(x):
g(x)
print(x)
Clearly, this can be done by monkey patching f. This however is not always practical, especially if f is some larger function as I would have to copy large portions of code (if this is even legal) just to change a single line. By injecting a call tog given some reference, for example "after line XXX" or "after instruction XXX" I could easily modify the behavior of f.
Is there a way to inject a function call into a different function? You can assume, that I know the source of f or at least that f is known well enough and unchanging for this to be possible.
f_patched?matplotlibthat could have easily been a lot more customizable with just a few extra lines of code...