What is a 'Closure'?

前端 未结 23 1237
星月不相逢
星月不相逢 2020-11-22 08:02

I asked a question about Currying and closures were mentioned. What is a closure? How does it relate to currying?

23条回答
  •  失恋的感觉
    2020-11-22 08:59

    A closure is a function that can reference state in another function. For example, in Python, this uses the closure "inner":

    def outer (a):
        b = "variable in outer()"
        def inner (c):
            print a, b, c
        return inner
    
    # Now the return value from outer() can be saved for later
    func = outer ("test")
    func (1) # prints "test variable in outer() 1
    

提交回复
热议问题