Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Closure (computer programming)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Anonymous functions == {{further|Anonymous function}} The term ''closure'' is often used as a synonym for [[anonymous function]], though strictly, an anonymous function is a function [[Literal (computer programming)|literal]] without a name, while a closure is an instance of a function, a [[Value (computer science)|value]], whose non-local variables have been bound either to values or to [[Variable (computer science)|storage locations]] (depending on the language; see the [[#Lexical environment|lexical environment]] section below). For example, in the following [[Python (programming language)|Python]] code: <syntaxhighlight lang="python"> def f(x): def g(y): return x + y return g # Return a closure. def h(x): return lambda y: x + y # Return a closure. # Assigning specific closures to variables. a = f(1) b = h(1) # Using the closures stored in variables. assert a(5) == 6 assert b(5) == 6 # Using closures without binding them to variables first. assert f(1)(5) == 6 # f(1) is the closure. assert h(1)(5) == 6 # h(1) is the closure. </syntaxhighlight> the values of <code>a</code> and <code>b</code> are closures, in both cases produced by returning a [[nested function]] with a free variable from the enclosing function, so that the free variable binds to the value of parameter <code>x</code> of the enclosing function. The closures in <code>a</code> and <code>b</code> are functionally identical. The only difference in implementation is that in the first case we used a nested function with a name, <code>g</code>, while in the second case we used an anonymous nested function (using the Python keyword <code>lambda</code> for creating an anonymous function). The original name, if any, used in defining them is irrelevant. A closure is a value like any other value. It does not need to be assigned to a variable and can instead be used directly, as shown in the last two lines of the example. This usage may be deemed an "anonymous closure". The nested function definitions are not themselves closures: they have a free variable which is not yet bound. Only once the enclosing function is evaluated with a value for the parameter is the free variable of the nested function bound, creating a closure, which is then returned from the enclosing function. Lastly, a closure is only distinct from a function with free variables when outside of the scope of the non-local variables, otherwise the defining environment and the execution environment coincide and there is nothing to distinguish these (static and dynamic binding cannot be distinguished because the names resolve to the same values). For example, in the program below, functions with a free variable <code>x</code> (bound to the non-local variable <code>x</code> with global scope) are executed in the same environment where <code>x</code> is defined, so it is immaterial whether these are actually closures: <syntaxhighlight lang="python"> x = 1 nums = [1, 2, 3] def f(y): return x + y map(f, nums) map(lambda y: x + y, nums) </syntaxhighlight> This is most often achieved by a function return, since the function must be defined within the scope of the non-local variables, in which case typically its own scope will be smaller. This can also be achieved by [[variable shadowing]] (which reduces the scope of the non-local variable), though this is less common in practice, as it is less useful and shadowing is discouraged. In this example <code>f</code> can be seen to be a closure because <code>x</code> in the body of <code>f</code> is bound to the <code>x</code> in the global namespace, not the <code>x</code> local to <code>g</code>: <syntaxhighlight lang="python"> x = 0 def f(y): return x + y def g(z): x = 1 # local x shadows global x return f(z) g(1) # evaluates to 1, not 2 </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Closure (computer programming)
(section)
Add topic