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))...
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
I don't know why you generate to many functions to begin with. There is a simple version of your code:
The lambda function you create builds a closure over the
composed
variable:This means that
composed[-1]
isn't evaluated when you create the lambda function, but when you call it. The effect is, thatcomposed[-1]
will be calling itself recursively again and again.You can solve this problem by using a helper function (with its own scope) to create the lambda functions: