|
发表于 2024-7-12 20:22:15
|
显示全部楼层
本楼为最佳答案
a(1)
a是outer函数的返回值
也是嵌套函数outer中的call_func
a == call_func
参数应该写在call_func之中
为了防止参数个数不同
call_func(*b)可变参数打包
inner_fun(*b)对多个参数解包引用给my_fun(n,m)
- def outer(inner_fun):
- def call_func(*b):
- print('这是装饰函数的开始')
- inner_fun(*b)
- print('这是装饰函数的结束')
- return call_func
- def my_fun(n,m):
- print(f'{n},{m}--这是实际函数执行的内容-my_fun')
- def he_fun(s):
- print(f'{s}--这是实际函数执行的内容-he_fun')
- a = outer(my_fun)
- b = outer(he_fun)
- a(1,2)
- a(2,3)
- b(4)
- b(5)
- print("-----------------------------------")
- #错误,没有参数s
- b()
复制代码
上方是演示内容
你要的
- def outer(inner_fun):
- def call_func(*b):
- print('这是装饰函数的开始')
- inner_fun(*b)
- print('这是装饰函数的结束')
- return call_func
- def my_fun(s):
- print(f'{s}--这是实际函数执行的内容')
- a = outer(my_fun)
- a(1)
复制代码 |
|