Python full stack development-Day9 function object, function nesting, namespace and scope, python-day9
I. Function objects
1. functions are the first type of objects, that is, functions can be passed as data.
1 can be referenced 2 can be passed as a parameter 3 return value can be an element of function 4 can be used as a container type
Ii. Use this feature to elegantly replace if
1 def foo (): 2 print ('foo') 3 4 def bar (): 5 print ('bar') 6 7 dic = {8 'foo': foo, # foo is the memory address 9 'bar': bar, # bar is the memory address 10} 11 while True: 12 choice = input ('>> :'). strip () 13 if choice in dic: 14 dic [choice] ()
Ii. Function nesting
1. nested function calls
Other functions are called in the function.
1 def max(x,y):2 return x if x > y else y3 4 def max4(a,b,c,d):5 res1=max(a,b)6 res2=max(res1,c)7 res3=max(res2,d)8 return res39 print(max4(1,2,3,4))
Ii. nested Function Definition
Define other functions in the function
1 def f1 (): 2 def f2 (): 3 def f3 (): 4 print ('from f3 ') 5 f3 () 6 f2 () 7 8 f1 () 9 f3 () # An error is reported, which cannot be called from outside. You can consider how to use functions in the function outside.
Iii. namespace and scope
1. What is a namespace?
Namespace: the place where the name is stored. There are three namespaces. (the previous problem x = is stored in the memory. Where is the name x stored? The namespace stores the binding relationship between name x and 1)
Ii. namespace Classification
#1. built-in namespace: # stores the name of the python interpreter. It takes effect when the interpreter is started, and the interpreter is disabled. #2. Global namespace: # a file-level name that takes effect when the file is executed. If the file is deleted after the file is executed #3. local namespace: # The name defined in the function (the function parameters and the name in the function are both stored in the local namespace). It takes effect temporarily when the function is called and expires when the function ends.
Iii. namespace loading sequence (built-in> global> Local)
Python test. py #1. The python interpreter is started first, so the built-in namespace is loaded first. 2. Run test. py file, and then load the global namespace Based on the file #3. If a function is called during file execution, a local namespace is generated temporarily.
4. search order of names (local> global> built-in)
# Local namespace ---> global namespace ---> built-in namespace # note that local files cannot be viewed globally, and global files can be viewed locally, example # max = 1def f1 (): # max = 2 def f2 (): # max = 3 print (max) f2 () f1 () print (max)
V. Scope
#1. Scope # global scope (including the names of built-in namespaces and global namespaces): # features: #1. Accessible at any location #2. the name in the range will be accompanied by the entire life cycle of the Program # (Global Survival, global validity) # local scope (name of a local namespace): # features: #1. It can only be used in a function. #2. It takes effect when the function is called and expires when the call ends. # (temporary survival, partially valid) #2. The scope relation is fixed in the function definition stage and irrelevant to the function call location. The following x = 1def f1 (): def f2 (): print (x) return f2x = 100def f3 (func): x = 2 func () x = Nation f3 (f1 () #3. View scope: globals (), locals () LEGB indicates the name search sequence: locals-> enclosing function-> globals-> _ builtins _ locals is the namespace in the function, including the local variables and the form parameter enclosing the name space of the external nested function (common in closures) globals global variables, the name space of the module where the function definition is located builtins the name space of the built-in Module
Iv. Closure Functions
1. What is a closure?
# Define a function inside the function, and the function contains a reference to the name in the scope of the external function. This function is a closure function. # tip: We used to pass external values to the function through parameters, the closure provides another way of thinking: Package, package, and package wow def outter (): x = 2 def inner (): # x = 1 print ("from inner ", x) return innerf = outter () f () # The value of x is 2.
Ii. Significance and Application of closures
# Significance of the closure: The returned function object is not only a function object, but also contains a layer of scope outside the function, so that the function is called no matter where it is, use the scope of your outer package first # application field: "crawler" (we used to pass the parameter, now we pack it) import requests def outter (url): # url = 'https: // www.baidu.com 'def get (): response = requests. get (url) if response. status_code == 200: print (response. text) return getbaidu = outter ('https: // www.baidu.com ') python = outter ('https: // www.python.org') baidu () python ()