closures (Closure)
In computer science, closures (English: Closure), also known as lexical closures (Lexical Closure) or function closures (functions closures), are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created.
Namespaces and Scopes
We can think of namespaces as a large dictionary type (Dict) that contains the mappings of the names and values of all variables. In Python, scopes can actually be seen as "rules for namespace variables at the location of the current context." At any point in the Python code execution, there are at least three levels of nested scopes:
- Local most inner scope, earliest search, including all local variables * * (Python default all variable declarations are local variables) * *
- Non-local the scope of all the outer functions that contain the current context, searching from the inside out, including non-local and non-global variables
- Global continues to search up until the current module is globally variable
- built-in outermost, last searched, built-in (built-in) variable
At any execution location, you can think of a scope as a search for a namespace such as the following:
scopes = { Span class= "pl-s" > "Local": {none, Non-local" Locals "Global": { "Locals": none, Built-in" Built-ins "}}},}
|
In addition to the default local variable declaration, Python has global
nonlocal
two types of declarations ( nonlocal
Python 3.x, 2.7 not), where global
the specified variable points directly to (3) the global variables of the current module. And nonlocal
then point to (2) the variable within the inner layer global
. The reason for the emphasis here (references and assignments) is that ordinary local variables have access to only read-only (read-only) * * * Outside the inner local scope, such as the following example:
>>>x = 100>>def main (): X + = 1 print (x) >>>main () Traceback (most recent call last): File " Span style= "COLOR: #800000" ><pyshell#50> ", line 1, in <module> main () File " <PYSHELL#49> " , line 2, Span style= "COLOR: #0000ff" >in main x + = 1 Unboundlocalerror:local variable " x " referenced before assignment
This UnboundLocalError
is thrown because main()
the scope inside the function has read-only access to the global variables, wants to be x
changed in main()
x
-pair, does not affect global variables, but creates a new local variable, and obviously cannot be used directly for local variables that have not yet been created x += 1
. If you want to get a full reference to a global variable, you need to global
declare:
>>>x = 100>>>def Main (): Global x + = 1 Print (x) >>>Main ()print# global variable has been changed 101
python closure Exercises
Here we have basically understood the rules for Python scopes, and we are writing a closure of a counter with JavaScript:
"" "/* JavaScript Closure Example */var inc = function () { var x = 0; return function () { Console.log (x + +); }; }; var inc1 = Inc ()var inc2 = Inc () "" "
# Python 3.6 def Inc (): x = 0 def inner (): nonlocal x x + = 1 print (x) return innerinc1 = Inc () INC2 = Inc () inc1 () inc1 () inc1 () Inc2 () 1231
In the above example, the Global environment inc1()
is executed, although the global environment is not able to get down to the inc()
local variables in x
, but we return an inc()
internal function inner()
, and the inner()
inc()
local variables in the access to the. That inner()
is to say inc()
, the local scopes within are packaged and given, so that inc1
inc2
they each have a closed scope independent of the global variables or any other operating environment, so they are called closures.
A closure function has a __closure__
property that contains the variables in the upper scope that it refers to:
Print (INC1. __closure__ [0].cell_contents] Print (INC2. __closure__ [0].cell_contents] [3] [1]
Original source: Rainyear
Reference
- 9.2. Python Scopes and namespaces
- Visualize Python Execution
- Wikipedia::closure
Python: Closures