标签:查询 比较 bre closure request 函数定义 命令 地址 fun
精髓:把函数当成变量去用,具体可以分为四个方面的使用
f=func
print(f,func)
f()
def foo(x): # x = func的内存地址
# print(x)
x()
foo(func) # foo(func的内存地址)
def foo(x): # x=func的内存地址
return x # return func的内存地址
res=foo(func) # foo(func的内存地址)
print(res) # res=func的内存地址
res()
l=[func,]
# print(l)
l[0]()
dic={‘k1‘:func}
print(dic)
dic[‘k1‘]()
函数对象应用示范:
def login():
print(‘登录功能‘)
def transfer():
print(‘转账功能‘)
def check_banlance():
print(‘查询余额‘)
def withdraw():
print(‘提现‘)
def register():
print(‘注册‘)
func_dic = {
‘0‘: [‘退出‘, None],
‘1‘: [‘登录‘, login],
‘2‘: [‘转账‘, transfer],
‘3‘: [‘查询余额‘, check_banlance],
‘4‘: [‘提现‘, withdraw],
‘5‘: [‘注册‘, register]
}
# func_dic[‘1‘]()
while True:
for k in func_dic:
print(k, func_dic[k][0])
choice = input(‘请输入命令编号:‘).strip()
if not choice.isdigit():
print(‘必须输入编号,傻叉‘)
continue
if choice == ‘0‘:
break
# choice=‘1‘
if choice in func_dic:
func_dic[choice][1]()
else:
print(‘输入的指令不存在‘)
def max2(x,y):
if x > y:
return x
else:
return y
def max4(a,b,c,d):
# 第一步:比较a,b得到res1
res1=max2(a,b)
# 第二步:比较res1,c得到res2
res2=max2(res1,c)
# 第三步:比较res2,d得到res3
res3=max2(res2,d)
return res3
res=max4(1,2,3,4)
print(res)
def f1():
def f2():
pass
def circle(radius,action=0):
from math import pi
def perimiter(radius):
return 2*pi*radius
# 求圆形的求面积:pi*(radius**2)
def area(radius):
return pi*(radius**2)
if action == 0:
return 2*pi*radius
elif action == 1:
return area(radius)
circle(33,action=0)
def f1():
x = 33333333333333333333
def f2():
print(x)
f2()
x=11111
def bar():
x=444444
f1()
def foo():
x=2222
bar()
foo()
def f1():
x = 33333333333333333333
def f2():
print(‘函数f2:‘,x)
return f2
f=f1()
# print(f)
# x=4444
# f()
def foo():
x=5555
f()
foo()
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):
# url=‘https://www.baidu.com‘
def get():
response=requests.get(url)
print(len(response.text))
return get
标签:查询 比较 bre closure request 函数定义 命令 地址 fun
原文地址:https://www.cnblogs.com/Franciszw/p/12550084.html