How do nested functions work in Python?

后端 未结 9 499
春和景丽
春和景丽 2020-12-01 04:50
def maker(n):
    def action(x):
        return x ** n
    return action

f = maker(2)
print(f)
print(f(3))
print(f(4))

g = maker(3)
print(g(3))

print(f(3)) # stil         


        
9条回答
  •  悲哀的现实
    2020-12-01 05:37

    That is what's called "closure". Simply put, for most if not all programming languages that treat functions as first-class object, any variables that are used within a function object are enclosed (i.e. remembered) so long as the function is still alive. It is a powerful concept if you know how to make use of it.

    In your example, the nested action function uses variable n so it forms a closure around that variable and remembers it for later function calls.

提交回复
热议问题