Python 难以定义高阶函数来模拟if语句

Python 难以定义高阶函数来模拟if语句,python,if-statement,higher-order-functions,Python,If Statement,Higher Order Functions,我很难回答我们最近在Python高阶函数练习中被问到的一个问题 问题是定义两个函数,其中一个不带参数,通过if/else语句传递三个全局定义的函数c()、t()和f()(ifc()true,returnt()else returnf())。另一个函数是我们对c()、t()和f()求值的高阶函数,然后通过相同的if/else语句传递它们 这些函数是不同的,我们的任务是通过定义三个函数c()、t()和f()来了解如何使第一个函数返回1,第二个函数返回除1以外的其他值 到目前为止,我已经意识到问题在于

我很难回答我们最近在Python高阶函数练习中被问到的一个问题

问题是定义两个函数,其中一个不带参数,通过if/else语句传递三个全局定义的函数c()、t()和f()(if
c()
true,return
t()
else return
f()
)。另一个函数是我们对
c()
t()
f()
求值的高阶函数,然后通过相同的if/else语句传递它们

这些函数是不同的,我们的任务是通过定义三个函数
c()
t()
f()
来了解如何使第一个函数返回1,第二个函数返回除1以外的其他值

到目前为止,我已经意识到问题在于在通过if/else语句传递函数之前调用函数
c()
t()
f()
。然而,这还不足以激发解决方案。有人能把我引向正确的方向吗

以下是相关代码:

def if_function(condition, true_result, false_result):

    if condition:
        return true_result
else:
    return false_result


def with_if_statement():

    if c():
        return t()
    else:
        return f()

def with_if_function():

    return if_function(c(), t(), f())

def c():
    return []

def t():
    return 1

def f():
    return 1

您可以轻松地将callable作为函数参数传递,而无需调用它们

def cond():
    return True

def f():
    return 2

def g():
    time.sleep(60)

def if_function(condition_callable, call_if_true, call_if_false):
    if condition_callable():
        return call_if_true()
    else:
        return call_if_false()

if_function(cond, f, g)  # evaluates immediately, does not sleep since g is never evaluated.

我在遵循要求方面遇到了困难。不清楚“无参数[函数处理]全局函数”会显示什么(既然如此,为什么要编写函数?);另外,“高阶函数[…]的评估”是什么意思?您能给出一些函数使用和预期结果的例子吗?