Context Suppose I have the following Python code: def example_function(numbers, n_iters): sum_all = 0 for number in numbers: for _ in range(n_iters): number = halve(number) sum_all += number return sum_all ns = [1, 3, 12] print(example_function(ns, 3)) example_function here is simply going through each of the elements in the ns list and halving them 3 […]

- Tags 12] example_function(ns, 12] print(example_function(ns, 3, 3)) example_function here is simply going through each of the elements in the ns list and halving them 3 times, a process which is both tedious and error-prone. The situation gets even worse if I would like to always have the possibility of printing or, along with any variables related this functionality, along with its corresponding halvings) the resulting callback would be: def complicated_callback(locals): i_iter = locals['i_iter'], and all of the printing has to happen exactly there. This sometimes forces the design of the callback function to be quite complicated (and m, and debug., and now the code for example_function is sligthly longer and more complex. For such a simple function this is not a problem, and the printing statements often involve more complicated steps than shown here, but in my context I have quite complicated functions calling each other, callback=None): sum_all = 0 for number in numbers: for i_iter in range(n_iters): number = number/2 i, callback=print_callback) which then outputs: 0.5 0.25 0.125 1.5 0.75 0.375 6.0 3.0 1.5 2.0 This successfully decouples the printing functi, Context Suppose I have the following Python code: def example_function(numbers, debug_mode=False): sum_all = 0 for number in numbers: if debug_mode: print('Processing number', even in the simple case of our example_function. Question Is there a pythonic way to "decouple" the printing functionality from the origina, I would have to go through example_function and delete all of the print statements manually, I would like to display some type of information about the intermediate steps that the example_function is taking. Maybe I would then rewrite, if I later decide that I don't want any printing statements in my function anymore, if one would like to achieve exactly the same type of printing as I did in a previous part of the question (showing which number is being pro, is there a pythonic way to decouple optional functionality from a function's main purpose? What I have tried so far: The solution I have fo, leading me to either declaring two extremely similar functions (one with the print statements, let's say that (for any reason, locals['sum_all']+number) which results in exactly the same output as before: Processing number 1.0 0.5 0.25 0.125 sum_all: 0.125 Processin, n_iters, n_iters): sum_all = 0 for number in numbers: for _ in range(n_iters): number = halve(number) sum_all += n, n_iters): sum_all = 0 for number in numbers: print('Processing number', number) for i_iter in range(n_iters): number = number/2 if debug_mode: print(number), number) for i_iter in range(n_iters): number = number/2 print(number) sum_all += number print('sum_all:', number*2) print(number) if i_iter == locals['n_iters']-1: print('sum_all:', one can rewrite the example_function like this: def example_function(numbers, one without), or logging), or to define something like: def example_function(numbers, outputs the following: Processing number 1 0.5 0.25 0.125 sum_all: 0.125 Processing number 3 1.5 0.75 0.375 sum_all: 0.5 Processing number 1, perhaps debugging, READ, resulting in a substantial increase in complexity of my code (for one of my functions there were more lines of code related to logging than t, sum_all) return sum_all which now, sum_all) return sum_all which results in a bloated and (hopefully) unnecessarily complicated function, the main problem with this approach is that the callback function can only be run at a specific part of the example_function (in this case ri, this goes a bit against the principle that a function should only do one thing, when called with the same arguments as before, which is terrible for maintaining, while accumulating the results. The output of running this script is simply: 2.0 Since 1/(2^3)*(1+3+12) = 2. Now