I'd like to get control (to execute some pre-emptive tasks) when a function is called in Python without modifying the source program, e.g., when calling test()
def test(i : int, s: str) -> int:
pass
I'd like a function myobserver to be called, and have some way to inspect (maybe even modify?!) the parameters? Think of it sorta like a mini-debugger, e.g., to add logging to an existing program that can't/shouldn't be modified?
def myobserver(handle)
name = get_name(handle)
for n, arg in enumerate(get_arg_iterator(handle)):
print("Argument {n} of function {name}: {arg}")
ETA: I am not looking for the traditional decorator, because adding a decorator requires changing the source code. (In this sense, decorators are nicer than adding a print, but still similar because they require changes to source.)
Your are looking for python decorators:EDIT
Since the decorator method mentioned above interferes with the source code (have to apply the
@decorators), I propose the following:What I propose is perform some manual effort in writing the hooks, I am not sure if this is feasible (just occured to me, hence adding):