Why "RecursionError: maximum recursion depth exceeded" occurred

Why "RecursionError: maximum recursion depth exceeded" occurred

我正在查看这个代码挑战:

Define a function cycle that takes in three functions f1, f2, f3, as arguments. cycle will return another function that should take in an integer argument n and return another function. That final function should take in an argument x and cycle through applying f1, f2, and f3 to x, depending on what n was.

Here's what the final function should do to x for a few values of n:

  • n = 0, return x

  • n = 1, apply f1 to x, or return f1(x)

  • n = 2, apply f1 to x and then f2 to the result of that, or return f2(f1(x))

  • n = 3, apply f1 to x, f2 to the result of applying f1, and then f3 to the result of applying f2, or f3(f2(f1(x)))

  • n = 4, start the cycle again applying f1, then f2, then f3, then f1 again, or f1(f3(f2(f1(x)))) And so forth.

下面是我的代码,但是在add_one_then_double的情况下会出现以下错误:

RecursionError: maximum recursion depth exceeded

有人可以帮我解决这个问题吗?谢谢!

def cycle(f1, f2, f3):

    """
    >>> def add1(x):
    ...     return x + 1
    >>> def times2(x):
    ...     return x * 2
    >>> def add3(x):
    ...     return x + 3
    >>> my_cycle = cycle(add1, times2, add3)
    >>> identity = my_cycle(0)
    >>> identity(5)
    5
    >>> add_one_then_double = my_cycle(2)
    >>> add_one_then_double(1)
    4
    >>> do_all_functions = my_cycle(3)
    >>> do_all_functions(2)
    9
    >>> do_more_than_a_cycle = my_cycle(4)
    >>> do_more_than_a_cycle(2)
    10
    >>> do_two_cycles = my_cycle(6)
    >>> do_two_cycles(1)
    19
    """
    def cycle_func(n):
        i=1
        result=lambda x:x
        while i<n+1:
            if i%3==1:
                result=lambda x: f1(result(x))
            elif i%3==2:
                result=lambda x: f2(result(x))
            else:
                result=lambda x: f3(result(x))
            i=i+1
        return result

    return cycle_func

问题是这样的语句:

result=lambda x: f1(result(x))

执行此赋值时,result 将被赋值这个新的 lambda 函数。稍后 executed 时,result(x) 将是对同一函数的引用,因此存在无限递归。在这里,您打算 引用 resultprevious 值,但如您所见,该值目前已丢失这项任务发生了。

解决方案是不要在那里创建那些“小”lambda 函数,而是创建一个获取 x 值的内部函数,然后将通过这种逻辑确定下一步要执行什么.

这是更正后的代码:

def cycle(f1, f2, f3):
    def cycle_func(n):
        def inner(x):
            i = 1
            result = x
            while i < n + 1:
                if i % 3 == 1:
                    result = f1(result)
                elif i % 3 == 2:
                    result = f2(result)
                else:
                    result = f3(result)
                i = i + 1
            return result

        return inner

    return cycle_func

def add1(x):
    return x + 1

def times2(x):
    return x * 2

def add3(x):
    return x + 3

my_cycle = cycle(add1, times2, add3)
identity = my_cycle(0)
print(identity(5)) # 5
add_one_then_double = my_cycle(2)
print(add_one_then_double(1)) # 4
do_all_functions = my_cycle(3)
print(do_all_functions(2)) # 9
do_more_than_a_cycle = my_cycle(4)
print(do_more_than_a_cycle(2)) # 10
do_two_cycles = my_cycle(6)
print(do_two_cycles(1)) # 19