compose
Alex Martelli
aleax at aleax.it
Mon May 5 03:54:30 EDT 2003
xam wrote:
> i've been truing to get this 'simple' code to work,
> def compose(*funcs):
> ... if len(funcs)>1:
> ... return lambda x:funcs[0](compose(funcs[1:])(x))
> ... else: return lambda x:funcs[0](x)
>>>> compose(add_5, mul_3, sub_2)(2)
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "<interactive input>", line 3, in <lambda>
> File "<interactive input>", line 4, in <lambda>
> TypeError: 'tuple' object is not callable
> does anyone want to give it a shot?
The function is strange, e.g. the else clause's body should
obviously be "return funcs[0]", but the problem is not there.
Rather, you have a problem of LEVELS: function compose wants
N arguments, each of them a callable, but in your recursive
call "compose(funcs[1:])" you are passing ONE argument, a tuple
of callables. Call "compose(*funcs[1:])" in order to fix that.
Alex
More information about the Python-list
mailing list