I would like to wrap some code around a function (e.g. print timing information) and reuse this function for multiple code blocks:
start = time.time()
print('Started adding numbers at {}'.format(start))
a = 1
b = 2
c = a + b
end = time.time()
print('Finished adding numbers in {} seconds'.format(end - start)')
Is it possible to wrap multiple lines of code into a wrapper function, without defining functions for each code blocks? One way would be to define a function and use a decorator but I would like to avoid doing this for every code block:
@print_time
def foo():
a = 1
b = 2
return a + b