赞
踩
精髓:可以把函数当做变量去用
- def func(): #func=函数的内存地址
- print('from func')
-
- f = func
- f() # from func
- 示例一:
- def fucn():
- print("from func")
-
-
- def bar(x):
- print(x) #m=11111
-
-
- mm = 11111
- bar(mm) #11111
-
- 示例二:
- def fucn():
- print("from fuunc")
- def bar(x):
- print(x) #func的内存地址:<function fucn at 0x000002358B9C3EE0>
- bar(fucn) #把func的内存地址给了bar
- bar(fucn()) #先调用fucn函数,拿到fucn的返回值在传给bar函数,返回结果:from fuunc、None,这里研究的是函数本身,而不是函数的返回值
-
- 实例三 在函数bar内调用了函数fucn
-
- def fucn():
- print("from fuunc")
- def bar(x):
- x() #from fuunc #触发函数体func的运行
- bar(fucn)
- 示例一:
- def func():
- print("from func")
- def add(x): # x=函数func的内存地址
- return x # return 函数func的内存地址
- res=add(func) # add(函数func的内存地址)
- print(res) #<function func at 0x000001D745153EE0>
-
- 实例二:
- def func():
- print('from func')
-
- def foo(x):
- return x
-
- res = foo(func)
- print(res) # <function func at 0x000001618F52CEE0>
- res() # from func
- 示例一:
- def func():
- print("from func")
- x=10
- l=[x,func]
- print(l) #[10, <function func at 0x00000191CFD83EE0>]
- l[-1]() #调用函数func的内存地址,而()触发函数体内的代码的运行,返回结果:from func
-
- 示例二:
- def func():
- print('from func')
-
- li = [func]
- print(li) # [<function func at 0x000001D99DC2CEE0>]
- li[0]() # from func
函数可以作为容器类型的元素应用列举
需求:模拟用户登录,注册。登录以后可以实现转账、查询余额、提醒、充值功能
- 示例一:代码的冗余性比较差
- def login():
- print("login")
- def register():
- print("register")
- def tranfer():
- print("transter")
-
- while True:
- print("""
- 0 退出
- 1 登录
- 2 注册
- 3 转账
- """)
- choice=input("请输入操作编号:").strip()
- if choice =="0":
- break
- elif choice =="1":
- login()
- elif choice== "2":
- register()
- elif choice =="3":
- tranfer()
- else:
- print("输入的操作不存在")
-
- 示例二:
- def login():
- print("login")
- def register():
- print("register")
- def tranfer():
- print("transter")
- func_dic={
- "1":login,
- "2":register,
- "3":tranfer
- }
- while True:
- print("""
- 0 退出
- 1 登录
- 2 注册
- 3 转账
- 4 提款
- """)
- choice=input("请输入操作编号:").strip()
- if choice =="0":
- break
- elif choice =="1":
- func_dic[choice]()
- else:
- print("输入的操作不存在")
- 示例三:
- def login():
- print("login")
- def register():
- print("register")
- def tranfer():
- print("transter")
- def withdraw():
- print('withdraw')
- func_dic={
- "1":[login,"登录"],
- "2":[register,"注册"],
- "3":[tranfer,"转账"],
- "4":[withdraw,"提现"]
- }
- while True:
- print("0 退出")
- for k in func_dic:
- print(k,func_dic[k][-1])
- choice=input("请输入操作编号:").strip()
- if choice =="0":
- break
- elif choice =="1":
- func_dic[choice][0]()
- else:
- print("输入的操作不存在")
-
- 实例四:
- def login():
- print("登录功能")
-
- def register():
- print("注册功能")
-
- def transfer_accounts():
- print("转账功能")
-
- def check_banlance():
- print("查询功能")
-
- def withdraw_deposit():
- print('提现')
-
- def recharge():
- print('充值')
-
- func_dic = {
- '0': ('退出', exit),
- '1': ('登录', login),
- '2': ('注册', register),
- '3': ('查询余额', check_banlance),
- '4': ('提线', withdraw_deposit),
- '5': ('充值', recharge),
- '6': ('转账', transfer_accounts),
- }
-
- while True:
- for key, value in func_dic.items():
- print(f'{key}:{value[0]}')
- cmd = input("输入命令编号>>:").strip()
- if not cmd.isdigit():
- print("请输入命令对应的数字编号")
- continue
- if cmd not in func_dic:
- print("输入命令编号范围超出")
- continue
- func_dic[cmd][1]()
在函数内定义其他函数
- def f1():
- print('from f1')
- def f2():
- print('from f2')
- f2()
- f1()
应用示例
使用场景:小功能组合
需求:求圆的周长和面积
周长(perimeter)
=2 * pi *
半径(radius)`
面积(area) = pi * 半径 ** 2
- 代码一:
- from math import pi #导入math模块
- def circle(radius,mode=0):
- def perimiter(radius): #计算周长函数
- return 2 * pi * radius
- def area(radius): #计算面积函数
- return pi * (radius ** 2)
- if mode ==0: #选0计算周长
- return perimiter(radius)
- elif mode == 1: #选1计算面积
- return area(radius)
- res=circle(10,mode=1) #传入半径10,选择1
- print(res) #314.1592653589793
-
- 代码二:
- def circle(radius, action=0):
- from math import pi
- def perimeter(radius):
- return 2 * pi * radius
- def area(radius):
- return pi * radius ** 2
- if action == 0:
- res = perimeter(radius)
- elif action == 1:
- res = area(radius)
- return res
- print(circle(3)) # 求圆的周长:18.84955592153876
- print(circle(3, 1)) # 求圆的面积:28.274333882308138
在调用一个函数的过程中又在他内部调用其他函数的组合(用一个小功能拼成一个大功能)
- # 比价两个值的大小
- def max2(a, b):
- if a > b:
- return a
- elif a < b:
- return b
- # 比较四个值的大小
- def maxs(a, b, c, d):
- res = max2(a, b)
- res = max2(res, c)
- return max2(res, d)
- print(maxs(1, 2, 3, 4)) # 4
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。