python怎么拼接两个函数

1267
2021/2/18 17:43:15
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python怎么拼接两个函数

在python中对两个函数进行拼接,具体方法如下:

1.首先,定义一个装饰工厂;

def decorator_factory(inner_func):

def decorator(outer_func):

def wrapper(*args, **kwargs):

return outer_func(*inner_func(*args, **kwargs))

return wrapper

return decorator

2.装饰工厂定义好后,通过使用装饰工厂进口实现函数拼接;

def f(a, b):

return a, b+2, b+3

@decorator_factory(f)

def g(a, b, c):

return a+b+c

print g(1, 2)

# output: 10

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python递归函数斐波那契数列怎么实现