map delaying function call


In this example we have added a call to print in the double function in order to see when is it really executed. You can see that the first output comes from the print statement that was after the map call. Then on each iteration we see the output from inside the "double" function and then the result from the loop. In a nutshell Python does not execute the "double" function at the point where we called map. It only executes it when we iterate over the resulting object.

examples/functional/map_with_print.py
def double(n):
    print(f"double {n}")
    return 2 * n

numbers = [1, 4, 2, -1]

double_numbers = map(double, numbers)
print(double_numbers)

for num in double_numbers:
    print(num)

<map object at 0x7f90df760f98>
double 1
2
double 4
8
double 2
4
double -1
-2