闭包定义和示例 [英] Closure Definition and Example

查看:109
本文介绍了闭包定义和示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是闭包及其对应的例子?



我有很多研究,不明白。
请在通用编程语言概念方面和特定编程语言方面进行说明。



请帮助。



谢谢。

解决方案

这里是我如何看待一个封闭....



函数对象由两个东西组成。第一件事是函数的代码,第二件是它执行的范围。在闭包中,函数执行的范围和代码彼此分离。相同的代码可以在各种范围内执行。



如果这是完全不受限制的方式允许的话,会导致很大的混乱。即使它像动态作用域那样松散(函数继承了调用它的地方的范围),它变得非常混乱。



闭包是一种方便的方法指定范围规则,这是很有意义的,因为他们只需要读取代码,而不是跟踪它。在闭包中,函数获取它被声明的范围。如果在执行另一个函数时声明它,它会获取函数堆栈的特定实例的作用域。



这是一个Python中的平凡闭包的例子:这是一个简单的例子,

  def outer():
def closure():
pass
return closure

这里函数 closure 是一个闭包。它实际上并不使用定义范围内的任何变量,所以它非常微不足道,但它仍然是一个。



这里是一个不是so-但在Python中仍然很简单的闭包:

  def outer(x):
def closure():
return x
return closure
f1 = outer(5)
f2 = outer(6)

调用 f1()将返回5并调用 f2() 。如你所见, closure 是部分代码,部分范围。



outer(5),它为调用创建一个堆栈条目,该条目包含保存值为5的变量 x 的版本。 $ outer(6) ,它为调用创建一个堆栈条目,该条目包含保存值为6的变量 x 的版本。然后声明函数 closure 即可获得该范围。


what is closure and its correspondence example ?

I have research a lot and could not understand. Please explain in generic programming language concept aspect and specific programming language aspect.

Please help.

Thanks.

解决方案

Here is how I think of a closure....

A function object consists of two things. The first thing is the code for the function, the second is the scope in which it executes. In a closure, the scope in which the function executes and the code are detached from each other. The same code can execute in a variety of scopes.

If this were allowed in a completely unrestricted way it would result in great confusion. Even when it's something as loose as dynamic scoping (the function inherits the scope of the place where it was called from) it becomes very confusing.

Closures are a convenient way of specifying scope rules that make a lot of sense because they only require reading the code instead of tracing it. In a closure, the function gets the scope of where it was declared. If it was declared while executing another function, it gets the scope of that specific instance of the function's stack. This is a lot simpler and easier to understand than being able to give the closure and arbitrary scope, or dynamic scoping.

Here is an example of a trivial closure in Python:

def outer():
    def closure():
        pass
    return closure

Here the function closure is a closure. It doesn't actually use any variables from the scope in which it was defined, so it's pretty trivial, but it still is one.

Here is a not-so-trivial, but still simple closure in Python:

 def outer(x):
      def closure():
          return x
      return closure
 f1 = outer(5)
 f2 = outer(6)

Calling f1() will return 5 and calling f2() will return 6. As you can see, the function closure is part code, and part scope.

When outer(5) is called, it creates a stack entry for the call that contains a version of the variable x holding the value 5. It then declares the function closure which gets that scope.

When outer(6) is called, it creates a stack entry for the call that contains a version of the variable x holding the value 6. It then declares the function closure which gets that scope.

这篇关于闭包定义和示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆