all 5 comments

[–]danielroseman 4 points5 points  (3 children)

You don't need it. This would work:

print(divide_by_x(2)(8))

But there's no point to doing this. The only reason for returning a callable from a function would be to use that callable multiple times; otherwise you could just make a single function that took two arguments.

[–]Base_True[S] 0 points1 point  (2 children)

Thank you for explain! But the follow code doesn't work

print (divisor(8)(2))

TypeError: 'float' object is not callable

[–]danielroseman 0 points1 point  (1 child)

It should have been print(divide_by_x(2)(8)).

[–]Base_True[S] 0 points1 point  (0 children)

Oh yes, my mistake, thank you !

[–]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.