用Python语法调用函数

2024-10-05 10:00:07 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我在用Python2.6编写一个小程序,我已经定义了 2个helper函数,它几乎可以完成我想要的所有功能,例如

def helper1:
    ...


def helper2:
    ...

现在我的问题是,我想创建一个新函数,将两个函数集合在一个函数中,这样我就不必写(在shell中):

^{pr2}$

但事实上

function(argument1,argument2)

有近路可走吗?我是python新手,还是需要更多的代码示例才能回答?在

如有任何提示或帮助,请提前谢谢


Tags: 函数程序功能helper定义deffunctionshell
3条回答
function = lambda x, y: list(helper1(helper2(x, y)))
def function(arg1, arg2):
    return list(helper1(helper2(arg1, arg2)))

应该行得通。在

这是高阶函数compose的一个例子。到处躺着很方便

def compose(*functions):
    """ Returns the composition of functions"""
    functions = reversed(functions)
    def composition(*args, **kwargs):
        func_iter = iter(functions)
        ret = next(func_iter)(*args, **kwargs)
        for f in func_iter:
            ret = f(ret)
        return ret
    return composition

现在可以将函数编写为

^{pr2}$

等等

相关问题 更多 >

    热门问题