|
发表于 2022-12-5 22:57:38
|
显示全部楼层
问答题答案:
# 1. 请将下面的 add() 函数修改为 lambda 表达式的表现形式?
# import functools
#
# def add(x, y):
# return x + y
#
# print(functools.reduce(add, range(1, 101)))
#
# print(functools.reduce(lambda x, y: x+y, range(1, 101)))
# 2. 偏函数的实现原理是?
# 多个参数的函数,为实现某项特定的功能,预先写入一些产生的值。
# 3. 请将下面闭包代码改用偏函数来实现?
# def power(exp):
# def exp_of(base):
# return base ** exp
# return exp_of
#
# square = power(2)
# cube = power(3)
# import functools
# square1 = functools.partial(pow, exp=2)
# cube1 = functools.partial(pow, exp=3)
# print(square1(2))
# print(square1(4))
# print(cube1(4))
# 4. 请使用偏函数,基于 print() 函数打造一个新的 pr() 函数,使其实现效果如下
# pr = functools.partial(print, sep='@',end='#')
# pr("I", "love", "FishC.")
# 5. 请修改下面代码,使得执行 test.__name__ 语句后,得到的结果是 'test',而非 'call_func'
# def myfunc(func):
# @functools.wraps(func)
# def call_func():
# func()
# return call_func
#
# @myfunc
# def test():
# print("test~")
#
# test()
#
# print(test.__name__)
动动手答案:
|
|