compose

Max Khesin max at cNOvSisiPonAtecMh.com
Mon May 5 10:56:24 EDT 2003


thank you (all) for the correction & a good observation. the corrected func
is:

def compose(*funcs):
...  if len(funcs)>1:
...   return lambda x:funcs[0](compose(*funcs[1:])(x))
...  else:
...   return funcs[0]

I am still learning and I could not find en extensive discussion of variable
args in the python docs. could you give me any good refs?
thanks-

--
========================================
Max Khesin, software developer -
max at cNvOiSsPiAoMntech.com
[check out our image compression software at www.cvisiontech.com, JBIG2-PDF
compression @
www.cvisiontech.com/cvistapdf.html]


"Alex Martelli" <aleax at aleax.it> wrote in message
news:WMota.74373$K35.2212502 at news2.tin.it...
> 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