L
L
linawhite2021-09-09 17:52:09
Python
linawhite, 2021-09-09 17:52:09

Explain how this code works, I don't understand these parenthesis manipulations?

def f():
    def a():
        def b():
            print("nested function")
        return b
    return a

f()()()

613a204a337b8658676486.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alan Gibizov, 2021-09-09
@linawhite

As a comment to all answers:
One must distinguish between a function object and a call to the result of a function evaluation.
The object is f
Calling the result is f()
This is where the trick is based.
f() becomes a
a() becomes b
b() becomes print("nested function")
This prints a string.

S
Sergo Zar, 2021-09-09
@Sergomen

613a23769c3ee335016512.png
Xs as it is called but here each bracket is responsible for each internal function in the order in which they are declared.

K
Kirill Petrov, 2021-09-09
@Recosh

In the code, the description of a function in a function... It's like a variable in a variable, only this function. As a result, the return of functions without their execution.
And then they are called with parentheses to execute them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question