Closure of Python functions (internal functions and external functions in detail)

Source: Internet
Author: User
Tags closure stdin
closure problems for Python functions (inline functions)
>>> def func1 ():
.     Print (' func1 running ... ')
...     Def func2 ():
.             Print (' Func2 running ... ')
...     Func2 () ... 
>>> func1 ()
func1 running ...
Func2 running ...

The internal function Func2 scopes are within the outer function func1 scope
If you try to call an internal function outside of an external function, you will get an error

>>> Func2 ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Nameerror:name ' FUNC2 ' is not defined
about the closure of Python

If you attempt to refer to a variable in an internal function that does not include an external scope of an external function, the intrinsic function is considered a closure

>>> def funcx (x):
...     def funcy (y):
...             Return x*y ...     Return funcy

For the Funcy function, reference is made to the variable x of the entire scope of the FUNCX function (the external role of the funcy function), since this can be said that the Funcy function is the so-called closure

>>> f = funcx (8)
>>> F
<function funcy at 0x7f3a436fc2a8>
>>> type (f)
<type ' function ' >
>>> F (Ten)
>>> Funcx (7) (8)
56

Since the closure itself is based on the concept of intrinsic functions, it is not possible to invoke internal functions in external scopes of external functions

>>> funcy (8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Nameerror:name ' funcy ' is not defined

Since it is based on the concept of internal functions, natural for internal functions to reference the scope of external functions of variables to modify, will start the interpreter's shielding mechanism

>>> def Func1 ():
.     x = 233 ...     Def Func2 ():
.             X *=x ...             return x ...     Return Func2 () ... 
>>> Func1 ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in Func1
  File "<stdin>", line 4, in FUNC2
unboundlocalerror:local variable ' x ' referenced before assignment

X*=x's left this time is a variable in the scope of the internal function, at which point an attempt is made to square the data without definition, so the error

>>> def Func1 ():
.     x = 233 ...     Def Func2 ():
.             x = 321 ...             return x ...     Return Func2 () ... 
>>> Func1 ()
321

Internal functions create X variables and mask the x variables within the scope of an external function

the solution before Python3

Apply a container type (such as list,tuple) a variable that holds the scope of an external function so that it is not shielded by the masking mechanism because the container type is not stored inside the stack

>>> def Func1 ():
.     x = [233]
...     Def Func2 ():
.             X[0] *= x[0]
...             return x[0]
...     Return Func2 () ... 
>>> Func1 ()
54289

Python3 after the solution: nonlocal keyword

>>> def Func1 ():
.     x = 233 ...     Def Func2 ():
.     nonlocal x ...             X *= x ...             return x ...     Return Func2 () ... 
>>> Func1 ()
54289
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.