On Jun 9, 3:29 am, Jussi Piitulainen <jpiit...@ling.helsinki.fi>
wrote:

> Here's something you could have thought of for yourself even when you
> didn't remember that Python does have special built-in support for
> applying a function to a list of arguments:
>
> def five(func, args):
>    a, b, c, d, e = args
>    return func(a, b, c, d, e)

>
> The point is that the function itself can be passed as an argument to
> the auxiliary function that extracts the individual arguments from the
> list.

Good point. However the function "five" is much too narrowly defined
and the name is atrocious! I like concise, self-documenting
identifiers.

py> L5 = [1, 2, 3, 4, 5]
py> L4 = [1, 2, 3, 4]
py> def f4(a,b,c,d):
        print a,b,c,d
py> def f5(a,b,c,d,e):
        print a,b,c,d,e
py> def apply_five(func, args):
        a, b, c, d, e = args
        return func(a, b, c, d, e)
py> apply_five(f5, L5)
1 2 3 4 5
py> apply_five(f5, L4)
ValueError: need more than 4 values to unpack
#
# Try this instead:
#
py> def apply_arglst(func, arglst):
        return func(*arglst)
py> apply_arglst(f4,L4)
1 2 3 4
py> apply_arglst(f5,L5)
1 2 3 4 5

...of course you could create a general purpose apply function; like
the one Python does not possess any longer ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to