Two syntax questions (newbie)
Szabolcs
szhorvat at gmail.com
Mon Apr 23 05:24:28 EDT 2007
Thanks for the reply!
On Apr 23, 10:55 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> > or define a
> > function composition operator for functions that take a single
> > argument?
>
> You could use this:
>
> def chain(*args):
> """Compose functions (assoc right).
> last argument (args[-1]): argument to last function
> args[0] .. args[-2]: functions taking a single argument
> Returns args[0](args[1](...(args[-2]))(args[-1])
> """
> args = list(args)
> data = args.pop(-1)
> while args:
> fn = args.pop(-1)
> data = fn(data)
> return data
>
> import random
> items = [random.randrange(100) for _ in range(20)]
> print chain(list, reversed, sorted, items)
This is already better. Is it possible to define function composition
as an operator and have something like (list at reversed@sorted)(items)
or (list*reversed*sorted)(items) ?
> I almost never use backslashes for line continuation. If you have an open
> (,[,{ continuation is implicit. If not, usually adding a heading ( is
> enough.
> That is, instead of
>
> x = a + b + \
> c - d
>
> I'd use:
>
> x = (a + b +
> c - d)
Thanks! This is very useful.
Szabolcs
More information about the Python-list
mailing list