I. Namespaces and Scopes
A, the namespace is divided into three kinds:
1. Global namespaces
2. Local namespaces
3. Built-in namespaces
Order of loading values for three namespaces
1. Global namespaces--run when running programs, not inside functions, loading sequentially from top to bottom
2. Local namespaces--the calling function is loaded when the program is running
3. Built-in namespaces--loaded before running the program
Order of values for three namespaces
In local call: local--Global-built
In global invoke: global--local
B, scope
Scopes are scoped and can be divided into global scope and local scope according to the effective scope
Global scope: Contains built-in namespaces, global namespaces, can be called anywhere in the entire file, and is globally valid
Local scope: Local namespace, only valid locally
Globals and Locals methods
Globals the variables in the function can change the value of the corresponding global variables.
Ii. nested and scoped chains of functions
A, nested invocation of a function
B, nested definitions of functions
C, the scope chain of the function
D, nonlcoal Keywords
# 1. External must have this variable
# 2. Variables with the same name can no longer appear before an intrinsic function declares a nonlocal variable
# 3. Internally modify this variable if you want to take effect in the first layer function that has this variable externally
Third, the essence of function name--function name is essentially the memory address of the function
1. Can be quoted
2. Elements that can be used as container types
3, can be used as a function of the parameters and return values (ordinary variables)
Four, closed package
Closure function:
An intrinsic function contains a reference to an outer scope rather than a full-action domain name, which is called a closure function (nesting of two or more two functions, and the function inside has a variable name for the function outside)
For example:
def func (): ' Eva ' def inner (): print (name)
#函数内部定义的函数称为内部函数
Because of the scope of the relationship, we cannot get the variables and functions inside the function. What if we just want to take it? Come back!
We all know the variables inside the function. If we want to use it outside the function, we can return the variable directly, so what if we want to call the function inside the function outside?
Would it be nice to return the name of the function directly?
This is the most common use of the closure function.
Closure function for network application
from urllib.request Import urlopendef index (): " http://www.xiaohua100.cn/index.html " get (): Return Urlopen (URL). Read () return Get== Xiaohua () print (content)
Python Learning Day11