def
outer_function
(x)
:
def
inner_function
(y)
:
return
x + y
return
inner_function
add_5 = outer_function(
5
)
result = add_5(
3
)
# 输出:8
def
process_data
(data, callback)
:
result = []
for
item
in
data:
result.append(callback(item))
return
result
def
square
(x)
:
return
x **
2
numbers = [
1
,
2
,
3
,
4
,
5
]
squared_numbers = process_data(numbers, square)
print(squared_numbers)
# 输出:[1, 4, 9, 16, 25]
from functools import partial
def power(base, exponent):
return base ** exponent
square = partial(power, exponent=2)
cube = partial(power, exponent=3)
result1 = square(4)
# 输出:16
result2 = cube(3)
# 输出:27
def
my_decorator
(func)
:
def
wrapper
()
:
print(
"在函数执行之前"
)
func()
print(
"在函数执行之后"
)
return
wrapper
def
say_hello
()
:
print(
"Hello!"
)
say_hello()
原文始发于微信公众号(编程者吧):Python学习 -- 高阶、闭包、回调、偏函数与装饰器探究
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论