Talk:Closure (computer programming): Difference between revisions

Content deleted Content added
Ruud Koot (talk | contribs)
Ruud Koot (talk | contribs)
Line 643:
 
: Also, closures can close over more than just the variables in the lexical scope, but other lexical entities entities (e.g. <code>this</code>, target of <code>return></code> and <code>break</code>, ...) as well. The current lead doens't mention this yet. —''[[User:Ruud Koot|Ruud]]'' 12:52, 2 April 2011 (UTC)
 
: To see a closure does not just "remember" values try:
<source lang="python">
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... x = 0
... def g():
... nonlocal x
... x = x + 1
... return x
... def h():
... nonlocal x
... x = x + 1
... return x
... return [g, h]
...
>>> [a,b] = f()
>>> a()
1
>>> a()
2
>>> a()
3
>>> b()
4
>>> b()
5
>>> a()
6
</source>
: —''[[User:Ruud Koot|Ruud]]'' 13:11, 2 April 2011 (UTC)