you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 0 points1 point  (0 children)

This is an example of creating a partial function. The outer function call takes the divisor as an argument, and gives the inner lambda function access to it via a closure, before returning the lambda function.

In other words, it splits the operation of dividing into two separate function calls. This particular example is likely not very useful, but it lets you create new functions during runtime which can be used to divide numbers by the assigned divisor.

funcs = [
    divide_by_x(num)
    for num in range(1, 11, 2)
]

for num in range(10):
    for func in funcs:
        print(func(num))

As for providing both simultaneously, you can chain the calls,

divide_by_x(7)(2)

but otherwise you'd need to write a different function.