hw01

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and false_result otherwise.
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_function():
    return if_function(c(), t(), f())

def c():
    return False

def t():
    print(5)

def f():
    print(6)

>>> result = with_if_function()
# 5
# 6
>>> print(result)   
# None
  • if_function 函数内的所有子函数会在 if_function 调用前计算出来(expression tree)

正确调用:

def with_if_statement():
    """
    >>> result = with_if_statement()
    6
    >>> print(result)
    None
    """
    if c():
        return t()
    else:
        return f()

with_if_statement()

Last updated

Was this helpful?