Highlights of this section: Namespaces and scopes, function nesting, function name essence, closures
Namespaces:
The nature of a namespace: a binding relationship between a name and a value.
Three types of namespaces:
Global namespaces
Local namespaces
Built-in namespaces
Order of loading and taking values between three namespaces:
Load order: Built-in namespaces (pre-Program load)-Global namespaces (program run: Load from top to bottom) local namespaces (program run: Load only when called)
Value:
Built-in namespaces, local namespaces, and global namespaces, local call spaces
Global invocation: Built-in namespaces, global namespaces
Scope:
Scope is the scope of action, according to the effective scope can be divided into global scope and local scope.
Global scope: Contains a built-in namespace, a global namespace, and can be referenced and globally valid anywhere in the entire file.
Local scope: Local namespace, which can only take effect within a local scope.
Globals and locals one is global, one is local.
Nested calls to functions:
def max2 (x, y ) : Mifelse y return mdef max4 (a,b,c,d): = max2 (A, b)= max2 ( res1,c)= Max2 (res2, d) return res3# max4 (23,-7,31,11)
nested calls to functions
def F1 (): Print ("inF1") def F2 (): Print ("inF2") F2 () F1 ()
nested definitions of functions (i)
def F1 (): def F2 (): def f3 (): Print ("inf3") Print ("inF2") f3 () print("inF1 ") F2 () F1 ( )
nested definitions of functions (ii)
The scope chain of the function:
def F1 (): = 1 def F2 (): print(a) F2 () F1 ()
scope Chain (i)
def F1 (): = 1 def F2 (): def f3 (): print(a) F3 () F2 () F1 ( )
scope Chain (ii)
def F1 (): = 1 def F2 (): = 2 f2 () print(' , a) F1 ()
scope Chain (iii)
nonlocal 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
def F1 (): = 1 def F2 (): nonlocal a = 2 f2 () print(' ', a) F1 ()
nonlocal Keywords
The nature of the function name:
The essence of a function name is the memory address of the stored function
1. Can be referenced
2. Elements that can be used as container types
3. Can be used as a function parameter and return value
Small summary: Just use it as a variable.
Closures:
An intrinsic function contains a reference to an outer scope rather than a global role name word, which is called a closure function.
def func (): ' Eva ' def inner (): Print (name) return = func () f ()
common usage of closure functions
#the __closure__ of the output has a cell element: a closure functiondeffunc (): Name='Eva' definner ():Print(name)Print(Inner.__closure__) returnInnerf=func () f ()#the output __closure__ is none: Not a closure functionName ='Egon'defFunc2 ():definner ():Print(name)Print(Inner.__closure__) returnInnerf2=Func2 () f2 ( )
determine if it is a closure
def wrapper (): = + def func (): 'Eva' def Inner (): Print (Name,money) return Inner return == f () I ()
Closure Nesting
from Import Urlopen def index (): " http://www.cnblogs.com/Eva-J/articles/7156261.html " def get (): return urlopen (URL). Read () return == Xiaohua ()print(content)
get web content with closures
Closure leads to the next lesson, adorners ...
Summarize:
Slightly
Python Learning: Function advanced