简体   繁体   English

Python函数组成(最大递归深度错误,范围?)

[英]Python function composition (max recursion depth error, scope?)

What is wrong with this function? 此功能有什么问题? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). 这似乎是一个范围错误(尽管我认为我已经通过将每个可调用对象放置在列表中而不是直接使用它来解决此问题)。 Error is max recursion depth reached (when calling comp(inv,dbl,inc))... 错误是达到最大递归深度(调用comp(inv,dbl,inc)时)...

Note: the question is: why is it even recursing, not why it's reaching the max depth... 注意:问题是:为什么还要递归,而不是为什么达到最大深度...

def comp(*funcs):
    if len(funcs) in (0,1):
        raise ValueError('need at least two functions to compose')
    # get most inner function
    composed = []
    print("appending func 1")
    composed.append(funcs[-1])
    # pop last and reverse
    funcs = funcs[:-1][::-1]
    i = 1
    for func in funcs:
        i += 1
        print("appending func %s" % i)
        composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))
    return composed[-1]

def inc(x):
    print("inc called with %s" % x)
    return x+1
def dbl(x):
    print("dbl called with %s" % x)
    return x*2
def inv(x):
    print("inv called with %s" % x)
    return x*(-1)

if __name__ == '__main__':
    comp(inv,dbl,inc)(2)

Traceback (if it helps): 追溯(如果有帮助):

appending func 1
appending func 2
appending func 3
Traceback (most recent call last):
  File "comp.py", line 31, in <module>
    comp(inv,dbl,inc)(2)
  File "comp.py", line 17, in <lambda>
    composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))
  File "comp.py", line 17, in <lambda>
    composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))
  File "comp.py", line 17, in <lambda>
    composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))
  (...)
  File "comp.py", line 17, in <lambda>
    composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))
RuntimeError: maximum recursion depth exceeded while calling a Python object

The lambda function you create builds a closure over the composed variable: 您创建的lambda函数会在composed变量的基础上建立一个闭包:

composed.append(lambda *args, **kwargs: func(composed[-1](*args,**kwargs)))

This means that composed[-1] isn't evaluated when you create the lambda function, but when you call it. 这意味着,当您创建 lambda函数时,不会评估composed[-1] ,但是会在您调用它时对其求值。 The effect is, that composed[-1] will be calling itself recursively again and again. 效果是, composed[-1]将一次又一次地递归调用自己。

You can solve this problem by using a helper function (with its own scope) to create the lambda functions: 您可以通过使用辅助函数(具有自己的作用域)来创建lambda函数来解决此问题:

def comp2(f1, f2):
    return lambda *args, **kwargs: f1(f2(*args, **kwargs))

...
for func in funcs:
     composed.append(comp2(func, composed[-1]))

I don't know why you generate to many functions to begin with. 我不知道为什么要生成许多函数。 There is a simple version of your code: 您的代码有一个简单的版本:

def compose(*funcs):
    if len(funcs) in (0,1):
        raise ValueError('need at least two functions to compose')

    # accepting *args, **kwargs in a composed function doesn't quite work
    # because you can only pass them to the first function.
    def composed(arg):
        for func in reversed(funcs):
            arg = func(arg)
        return arg

    return composed

# what's with the lambdas? These are functions already ...
def inc(x):
    print("inc called with %s" % x)
    return x+1
def dbl(x):
    print("dbl called with %s" % x)
    return x*2
def inv(x):
    print("inv called with %s" % x)
    return -x

if __name__ == '__main__':
    f = compose(inv,dbl,inc)
    print f(2)
    print f(3)

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

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