python怎么拼接两个函数 python的拼接函数

主机教程 建站分享 1年前 (2022-12-13) 130次浏览

文章摘要:python怎么拼接两个函数 python的拼接函数

在python中对两个函数进行拼接,具体方法如下: 1.首先,定义一个装饰工厂; def decorator_ […]

在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


声明:
若非注明,本站文章源于互联网收集整理和网友分享发布,如有侵权,请联系站长处理。
文章名称:python怎么拼接两个函数 python的拼接函数
文章链接:https://www.7966.org/post/14340.html
转载请注明出处

喜欢 (0)