Full-stack Python development: python function development (advanced) and python function development (advanced)
Python functions advanced function nesting function object namespace and scope closure function decorators 1. Function nesting
Nested Function Definition
Def func1 (): print ('from func1') def func2 (): # func2 = memory address print ('from func2') print (func2) func1 ()
Nested function call
# Compare the size of two numbers def max2 (x, y): if x> y: return x else: return y # compare the size of three numbers def max3 (x, y, z): res1 = max2 (x, y) res2 = max2 (res1, z) return res2print (max3 (11,199, 2 ))
# Result
#199
Ii. Function objects
Functions are the first type of objects in python.
1. can be referenced
X = 1y = xdef bar (): print ('from bar') f = bar # when an object is assigned a value, bar represents the memory address f ()
2. parameters can be passed in (the essence of the function name, the memory address of the function)
f=func
3. Possible return values of functions
def bar(x): print(x) x()bar(func)
4. Elements of the container type
def bar(x): # x=func return x #return funcres=bar(func) #res=func# print(res)res()
Iii. namespace and scope namespace: the space for storing names and binding relationships
There are three types of namespaces.
Built-in namespace: stores the name that comes with the Python interpreter. It takes effect when the interpreter is started. If the interpreter is disabled, it becomes invalid.
Global namespace: a file-level name that takes effect when the file is executed. It becomes invalid when the file is deleted or ended.
Local namespace: stores the name defined in the function (both the function parameters and the name in the function are stored in the local namespace). # It takes effect temporarily when the function is called and expires when the function ends.
The loading sequence and value sequence between the three namespaces:
Loading Sequence: built-in (loaded at interpreter startup, loaded before program running) --> global (loaded from top to bottom) --> Local (loaded at call) ---> built-in
Value: Local call: local namespace ---> global namespace ---> built-in namespace
Search globally:
Global ---- built-in ---- local usage:
Local
You can use the global
Scope: indicates the scope of action.
1. namespaces and scopes are inseparable.
2. There are two scopes:
Global scope: the names of both the global namespace and the built-in namespace belong to the global scope and can be referenced anywhere in the entire file.
Local scope: local namespace, which can only take effect within the local scope
3. Global View
If the name is globally available
If not, use the built-in
4. Why is there scope?
To ensure that the variables in the function do not affect the global
5,
Pass
Iv. Closure Functions
Closure: 1. Closed: internal function
2. Package: contains references to variables in the external function scope.
Def hei (): x = 20 def inner (): x = 10 # If x is defined, it will use its own, and the closure print (x) cannot be implemented)
# Common form of closure: def hei (): x = 20 def inner (): ''' closure function ''' print (x) return inner () common form of closure function
Method for judging closure functions: __closure __
# The output _ closure _ has a cell element: the closure function def func (): name = 'eva 'def inner (): print (name) print (inner. _ closure _) return innerf = func () f () # output _ closure _ is None: not the closure function name = 'egon' def func2 (): def inner (): print (name) print (inner. _ closure _) return innerf2 = func2 () f2 ()
View Code
Closures get network applications
# from urllib.request import urlopen# def index(url):# def inner():# return urlopen(url).read()# return inner# u='http://www.cnblogs.com/Eva-J/articles/7156261.html#_label1'# get = index(u)# print(get())
View Code 5. decorator
Pass