We can use a Nested function to avoid the decorator from being called implicitly by Python.
As you can see above, the only change that we have made is that we have added a nested function called wrapper()
and moved the code within it. measure_time()
returns this wrapper
function to the caller.
If we run the code snippet now, you will notice measure_time()
is not being called implicitly. Instead, we will have to call hello_world()
, which will internally call the code from wrapper()
within the measure_time
decorator.
Uncomment the last line to run hello_world()
and see the results.
This way we can go on to use hello_world()
as a usual independent function.