table of Contents
1, function object
Essence: You can go with a function as a variable, the function name is stored in the memory address
# func=内存地址
def func():
print('from func')
1.1 function object can be assigned
f=func
print(f,func)
f()
1.2 can be used as an argument to another function
def foo(x): # x = func的内存地址
# print(x)
x()# func()
foo(func) # foo(func的内存地址)
1.3 can be used as the return value of a function of further
def foo(x): # x=func的内存地址
return x # return func的内存地址
res=foo(func) # foo(func的内存地址)
print(res) # res=func的内存地址
res()
1.4 can be used as a type of container element
l=[func,]
# print(l)
l[0]()
dic={'k1':func}
print(dic)
dic['k1']()
1.5 Application example of function objects
#简单的ATM功能
def login():
print('登录功能')
def transfer():
print('转账功能')
def check_banlance():
print('查询余额')
def withdraw():
print('提现')
def register():
print('注册')
func_dic={
'1':login,
'2':transfer,
'3':check_banlance,
'4':withdraw,
'5':register
}
while True:
print("""
0 退出
1 登录
2 转账
3 查询余额
4 提现
5 注册
""")
choice = input('请输入命令编号:').strip()
if not choice.isdigit():
print('必须输入编号,傻叉')
continue
if choice == '0':
break
if choice in func_dic:
func_dic[choice]()
else:
print('输入的指令不存在')
#简单优化版
func_dic = {
'0': ['退出', exit],
'1': ['登录', login],
'2': ['转账', transfer],
'3': ['查询余额', check_banlance],
'4': ['提现', withdraw],
'5': ['注册', register]
}
while True:
for k in func_dic:
print(k, func_dic[k][0])
choice = input('请输入命令编号:').strip()
if not choice.isdigit():
print('必须输入编号,傻叉')
continue
if choice in func_dic:
func_dic[choice][1]()
else:
print('输入的指令不存在')
2, the nested function
2.1 nested function calls
In the process of calling a function and call other functions
def max2(x,y):
if x > y:
return x
else:
return y
def max4(a,b,c,d):
res1=max2(a,b)
res2=max2(res1,c)
res3=max2(res2,d)
return res3
res=max4(1,2,3,4)
print(res)
2.2 nested function definitions
Other functions defined in the function
def f1():
def f2():
pass
2.3 Application example of nested functions
# 圆形
def circle(radius,action=0):
from math import pi
# 求圆形的求周长:2*pi*radius
def perimiter(radius):
return 2*pi*radius
# 求圆形的求面积:pi*(radius**2)
def area(radius):
return pi*(radius**2)
if action == 0:
return perimiter(radius)
elif action == 1:
return area(radius)
circle(33,action=0)
3, the closure function
3.1 premise
Closure function namespace with a scope = + + function nested function object
core point: the name of the relationship is to find the function definition stage prevail
3.2 What is the function of closure
"Closed" function refers to the function is built-in functions
of the function "package" function refers to the outer layer contains a reference to the name of the function scope (not on the global scope)
3.2.1 Application Name Space and scope + nested functions
def f1():
x = 33333333333333333333
def f2():
print(x)
f2()
x=11111
def bar():
x=444444
f1()
def foo():
x=2222
bar()
foo()# 33333333333333333333
3.2.2 Application Name Space and scope + + nested function object function
def f1():
x = 33333333333333333333
def f2():
print('函数f2:',x)
return f2
f=f1()
print(f)# <function f1.<locals>.f2 at 0x000001C757F1A430>
x=4444
f() # '函数f2:33333333333333333333
def foo():
x=5555
f()
foo()
3.3 Why have a closure function
Two kinds of transmission parameters as a function of the body by:
Way: the parameter defines the function body directly molded required parameters
def f2(x):
print(x)
f2(1)
f2(2)
f2(3)
Second way: the closure function
def f1(x): # x=3
x=3
def f2():
print(x)
return f2
x=f1(3)
print(x)
x()
Application Example 3.4 closure function
import requests
# 传参的方案一:
def get(url):
response=requests.get(url)
print(len(response.text))
get('https://www.baidu.com')
get('https://www.cnblogs.com/linhaifeng')
get('https://zhuanlan.zhihu.com/p/109056932')
# 传参的方案二:
def outter(url):
def get():
response=requests.get(url)
print(len(response.text))
return get
baidu=outter('https://www.baidu.com')
baidu()
cnblogs=outter('https://www.cnblogs.com/linhaifeng')
cnblogs()
zhihu=outter('https://zhuanlan.zhihu.com/p/109056932')
zhihu()