温馨提示×

python怎么拼接两个函数

九三
456
2021-02-18 17:43:15
栏目: 编程语言

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

0