Python self-learning functions embedded and closed

Source: Internet
Author: User

Inside a function is a function that contains the definition of another function.
Example:

>> def fun1 ():
Print (' fun1 () is being called ... ')
Def fun2 ():
Print (' fun2 () is being called ... ')
Fun2 ()

>> fun1 ()
FUN1 () is being called ...
Fun2 () is being called ...
>> fun2 ()
Traceback (most recent):
File "<pyshell#7>", line 1, in <module>
Fun2 ()
Nameerror:name ' fun2 ' is not defined

Closure (closure) is an important grammatical structure of functional programming, and functional programming is a programming paradigm, and the well-known functional programming language is the Lisp language.
Closures in Python are defined as: if in an intrinsic function a reference is made to a variable that is in an outer scope (but not at the global scope), then the intrinsic function is considered a closed packet.
Example

>> def funx (x):
def funy (y)://funy is the intrinsic function of funx, the entire function of Funx is scoped to the outer scope of Funy, and the variable x
return x * y
Return Funy

>> i = Funx (8)
>> i//i is a function
<function Funx.<locals>. Funy at 0x029cdcd8>
>> type (i)
<class ' function ' >
>> I (5)
40
>> Funx (8) (5)
40
>> Funy (5)
Traceback (most recent):
File "<pyshell#31>", line 1, in <module>
Funy (5)
Nameerror:name ' Funy ' is not defined

Intrinsic functions can only be referenced to local variables of external functions and cannot be modified
Example:

>> def Fun1 ():
x = 5
Def Fun2 ():
X *= x//x is a local variable of an external function and can only be referenced without modification
return x
Return Fun2 ()

>> Fun1 ()
Traceback (most recent):
File "<pyshell#39>", line 1, in <module>
FUN1 ()
File "<pyshell#38>", line 6, in FUN1
Return Fun2 ()
File "<pyshell#38>", line 4, in Fun2
X *= x
unboundlocalerror:local variable ' x ' referenced before assignment
>>

Solutions
Method One:
Container type storage solution, container type is not stored in the stack

>> def Fun1 ():
x = [5]//Use a list to store X
Def Fun2 ():
X[0] *= x[0]
return x[0]
Return Fun2 ()

>> Fun1 ()
25
>>
Method Two
nonlocal keyword, 3.0 after version join
>> def Fun1 ():
x = 5
Def Fun2 ():
nonlocal x
X *= x
return x
Return Fun2 ()

>> Fun1 ()
25
>>

Python self-learning functions embedded and closed

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.