I know python is extremely flexible allowing -almost- anything user wants. However I have never seen nor heard of such a feature, and could not find anything related online: is it possible to execute a variable that is a function step by step?
def example_function():
print("line 1")
# stuff
print("line 2")
# stuff
return(3)
def step_by_step_executor(fn):
while fn.has_next_step():
print(fn.current_step)
fn.execute_step()
return fn.return
step_by_step_executor(example_function)
# print("line 1")
# line 1
# stuff
# print("line 2")
# line 2
# stuff
# return(3)
# returns 3
I think I can implement something like this using a combination of inspect, exec and maybe __call__, but I am interested to see if there is an already existing name and implementation for this.
Example use cases:
@do_y_instead_of_x
def some_function():
do stuff
do x
do more
some_function()
# does stuff
# does y
# does more
@update_progress_bar_on_loops
def some_other_function():
do stuff
for x in range...:
...
do more
some_other_function()
# does stuff
# initializes a progress bar, reports whats going on, does the loop
# does more
pdb.runcall.pdb.runcalllooks like it is specifically intended for debugging. I'm not sure if this would impose any performance drawbacks. (I will check and consider this as a possible solution when I start coding this)