Little Turtle Python After the 19th lecture exercise

Source: Internet
Author: User
Tags closure

NOTES: 1, inline function: Create another function inside function 2, closure: The important syntax of functional programming, if in an intrinsic function, the external scope (but not the global scope of the variable) is referenced, then the intrinsic function is considered a closure. 3. nonlocal: Used to declare a local variable test question 0. What keyword should you use if you want to modify the value of a global variable in a function? Global 1. In a nested function, what keyword should you use if you want to modify the local variables of an external function in an intrinsic function? nonlocal

2.Python functions can be nested, but pay attention to the scope of the access problem Oh, ask the following code what is the problem?

Def outside ():
Print (' I am outside! ')
Def inside ():
Print (' I am inside! ')
Inside ()

Inside () is an intrinsic function and cannot be called externally
One thing to note with nested functions is that the inside () function is embedded in the outside () function, so inside () is a wife who can touch (invoke) in addition to being a husband's outside (call), which cannot be invoked outside or in another function body.

3. Why did the code a not error, but the code b error? How should I modify it?
Code A:
Def outside ():
var = 5
Def inside ():
var = 3
Print (VAR)
Inside ()
Outside ()
Code B:
Def outside ():
var = 5
Def inside ():
Print (VAR)
var = 3
Inside ()
Outside ()

There is a var variable in the outside () function here, but note that the inline function inside () also has a variable with the same name, and Python is shielded from the Var variable of the outside () in order to protect the scope of the variable, so it is not possible to access the VAR variable at the outer layer. should be modified to:
Def outside ():
var = 5
Def inside ():
nonlocal var
Print (VAR)
var = 8
Inside ()
Outside ()

4. How do I access Funin ()?
Def funout ():
Def funin ():
Print (' Bingo! You have successfully visited me! ‘)
Return Funin ()

Funout ()

5. How do I access Funin ()?
Def funout ():
Def funin ():
Print (' Bingo! You have successfully visited me! ‘)
Return Funin

Funout () ()

6. Below is an example of a "closure", so what do you print out visually?
Def funx ():
x = 5
Def funy ():
nonlocal x
x + = 1
return x
Return Funy
A = Funx ()
Print (A ())
Print (A ())
Print (A ())

6
7
8
Some fish oil may be more confused, this ... What is the same as a global variable? The local variable x should not be reinitialized every time it is called?!

In fact, we have a closer look to understand that when a = Funx (), as long as the A variable is not re-assigned, Funx () is not released, that is, the local variable x is not reinitialized.
So when the global variables do not apply, you can consider the use of closures more stable and secure, you can also refer to the role of the game in motion: closure in the actual development of the role of




Little Turtle Python After the 19th lecture exercise

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.