First, the name of the keyword parameters:
What is a named keyword parameter?
Format: After * The parameters are all named keyword parameters
Characteristics:
1 must be passed to the value
1 The caller of the constraint function must pass the value in the form of Key=value
2 The caller of the constraint function must use the key name we specified
def foo (x,y,*,z): #创建foo函数, z for the named keyword parameter
Print (x, y, z)
#foo (1,2,aaa=3) #报错, z is a named keyword parameter and can only be z= value with the keyword
Foo (1,2,z=3)
——————————————————————————————
def auth (*args,name,pwd): #设定, name,pwd as the named keyword parameter
Print (NAME,PWD)
Auth (pwd= ' 213 ', name= ' Egon ') #输入关键字参输
Second, the function of nesting:
1, nested invocation of function: Another function called within a function
Def max2 (x, y): #判断两个值大小, returns the large value
if x > y:
return x
Else:
Return y
def max3 (x, Y, z): #用嵌套方法判断三个数的值
Res1=max2 (x, y)
Res2=max2 (res1,z)
return res2
Br>print (Max3 (11,199,2))
2, nested definition of function: Define other functions within function
Def func1 (): #定义func1函数
Print (' from func1 ')
def func2 (): #func2 = memory address #嵌套定义func2函数
Print (' from Func2 ')
Print (FUNC2) #<function func1. <locals>.func2 at 0x0000024907a098c8> #输出func2函数名地址
Func2 ()
Func1 ()
——————————————————— ———————————————
def f1 (): #嵌套定义, call F1 execution to derive values for all function outputs, all used
print (' F1 ')
def f2 ():
Print (' F2 ')
def f3 ():
Print (' F3 ')
F3 ()
F2 ()
F1 ()
"'
F1
F2
F3
'
Three, namespaces and scopes
A: namespace: where the name and value bindings are stored
x=1111111111
def func ():
Pass
Two: The name space is divided into three categories
1 Built-in namespaces: holds the Python interpreter's own name, takes effect when the interpreter starts, and fails when the interpreter is closed
2. Global namespace: The file-level name, which takes effect when the file is executed, expires at the end of the file or is deleted during file execution
3. Local namespace: The name defined in the store function (the parameters of the function and the names within the function are stored with the local namespace),
Temporary effect at function call, function end is invalid
Load order: Built-in namespaces-"global namespaces-" local namespaces
Find Name: Local namespace-global namespace-built-in namespaces
len= ' Global ' #调用f1, run to the F2 method output Len, look for values from the current outward, and finally output to global
Def f1 ():
# Len=1
def f2 ():
# len=2
Print (len)
F2 ()
F1 ()
Three: Scope
Global scope: Contains the name of the built-in namespace and the global namespace,
Characteristics
1 can be accessed at any location
2 names within this range will accompany the program throughout its life cycle
Local scope: Contains the name of the local namespace
Characteristics:
1, can only be used within the function
2. The function is called, and the call end is invalid.
Four, Function object
function is the first class of objects in Python?
1. Can be quoted
def bar (): #创建bar函数
Print (' from Bar ')
F=bar #引用bar函数
F ()
2, the parameters can be passed in
def bar (): #创建bar函数
Print (' from Bar ')
def wrapper (func): #func =bar #创建wrapper函数, set parameter func
Func () #bar ()
Wrapper (BAR) #把bar当做参数传入
3, can be used as the return value of the function
def bar (): #创建bar函数
Print (' from Bar ')
def foo (func): #func =<function Bar at 0x00000225af631e18> #创建foo函数, set variable func, return variable func
return func #return <function Bar at 0x00000225af631e18>
F=foo (bar) #f =<function Bar at 0x00000225af631e18> #把bar函数当做参数传入变量func to be the return value of the Foo function
F ()
4. Elements of the container type can be
Def get (): #创建get function
Print (' from Get ')
def put (): #创建put函数
Print (' from put ')
L=[get,put] #建立列表输入两个函数
L[0] () #安索引调用get函数
——————————————————————————————————
Cases:
def func ():
Print (' from Func ')
1. Func can be referenced
F=func
2. Func can be passed as a parameter to X
def bar (x):
Print (x)
X ()
Bar (func)
3. Func can also be used as a return value
def bar (x): # X=func
return x #return func
Res=bar (func) #res =func
Print (RES)
Res ()
4 elements that can be of the container type
def auth ():
Print (' Login ..... ‘)
Def reigster ():
Print (' Register .... ‘)
Def search ():
Print (' View .... ‘)
def transfer ():
Print (' Transfer .... ‘)
def pay ():
Print (' Pay .... ‘)
dic={
' 1 ': auth,
' 2 ': Reigster,
' 3 ': Search,
' 4 ': transfer,
' 5 ':p ay
}
Print (DIC)
dic[' 2 '] ()
def Interactive ():
While True:
Print ("" "
1 Certifications
2 Registration
3 views
4 transfer
5 Payment
""")
Choice=input (' >>: '). Strip ()
If choice in dic:
Dic[choice] ()
Else
Print (' illegal operation ')
Interactive ()
Five, closed packet function
1. Closure function
Closed: Refers to a function defined inside a function
!!! The scope relationship is dead in the function definition phase, regardless of the location of the call
Def outter (): #创建outter函数, create variable X
x=2
def inner (): #创建inner函数, Output X
# x=1
Print (' From inner ', x)
return inner #返回inner函数
F=outter () #f =inner #把返回的嵌套定义函数赋予f so that f can call a function that is nested defined.
F ()
Closure function:
1. Functions defined inside the function
2, and the function contains a reference to the name in the scope of the outer function, which is called the closure function
Def outter (): #定义函数outter, and define the variable name
Name= ' Egon '
def inner ():
Print (' My name is%s '%name) #定义函数inner, output name
return inner #返回函数inner
F=outter () #把嵌套定义函数inner赋值给f
Know:
How to pass a value to a function body
Method One: Passing the value in the form of a parameter
Import requests #闭合函数, transform variables, transform addresses
def get (URL):
Response=requests.get (URL)
if Response.status_code = = 200:
Print (Response.text)
Get (' https://www.baidu.com ')
Way Two
Import requests
Import time
def outter (URL): #url = ' https://www.baidu.com #定义函数outter, formal parameter URL
Def get (): #定义函数 get based on URL, fear URL data
Response=requests.get (URL)
if Response.status_code = = 200:
Print (Response.text)
return get #返回函数get
Baidu=outter (' https://www.baidu.com ') #把带实参百度地址的函数赋予baidu, you only need to run Baidu ()
Python=outter (' https://www.python.org ') #把带实参python地址的函数赋予python, you only need to run Python () in the future.
Python tour. Chapter III. function 3.28