简体   繁体   English

为什么发生“RecursionError:超出最大递归深度”

[英]Why "RecursionError: maximum recursion depth exceeded" occurred

I am looking at this code challenge:我正在看这个代码挑战:

Define a function cycle that takes in three functions f1 , f2 , f3 , as arguments.定义一个 function 循环,它接收三个函数f1f2f3 ,作为 arguments。 cycle will return another function that should take in an integer argument n and return another function.循环将返回另一个 function,它应该接受一个 integer 参数 n 并返回另一个 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.最后的 function 应该接受一个参数x并循环应用f1f2f3x ,这取决于n是什么。

Here's what the final function should do to x for a few values of n :下面是最终 function 对于n的几个值应该对x执行的操作:

  • n = 0, return x n = 0,返回x

  • n = 1, apply f1 to x , or return f1(x) n = 1,将f1应用于x ,或返回f1(x)

  • n = 2, apply f1 to x and then f2 to the result of that, or return f2(f1(x)) n = 2,将f1应用于x ,然后将f2应用于其结果,或返回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 = 3,将f1应用于x ,将f2应用于应用f1的结果,然后将f3应用于应用f2的结果,或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. n = 4,再次开始循环应用f1 ,然后是f2 ,然后是f3 ,然后是f1 ,或者f1(f3(f2(f1(x))))等等。

Below is my code, but the following error occurs for the case add_one_then_double :下面是我的代码,但是对于add_one_then_double的情况会出现以下错误:

RecursionError: maximum recursion depth exceeded RecursionError:超出最大递归深度

Could anybody help me on this?有人可以帮我吗? Thank you!谢谢!

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

The problem is statements like this one:问题是这样的陈述:

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

When this assignment is executed, result will be assigned this new lambda function.执行此分配时, result将分配给这个新的 lambda function。 When later it is executed , result(x) will be a reference to this same function, and thus there is an infinite recursion.稍后执行时, result(x)将是对同一 function 的引用,因此存在无限递归。 Here you intended to have a reference to the previous value of result , but as you can see, that was lost at the moment this assignment took place.在这里,您打算引用result先前值,但正如您所见,在此分配发生时该值已丢失。

The solution, is to not create those "little" lambda functions there, but to create one inner function that gets the value of x , and will then go through this logic of determining what to execute next.解决方案是不要在那里创建那些“小” lambda 函数,而是创建一个内部 function 来获取x的值,然后通过 Z34D1F91FB2E514B8576FAB1A75A89A6 确定下一个逻辑来执行什么。

Here is the corrected code:这是更正后的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM