function arguments

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Sep 15 16:23:57 EDT 2004


"Joe Laughlin" <Joseph.V.Laughlin at boeing.com> wrote in message
news:I43MHJ.66G at news.boeing.com...
> I want to do something like the following
>
> def foo(list_of_args):
>     call_other_function(arg1, arg2, arg3)
>     # Where arg1 == "x", arg2 == "y", etc.
>     # Should work with any list size
>
> foo(["x", "y", "z"])
>
> Make sense?  Need clarification?  In summary, I want to pass a list of
> arguments to a function.  The function needs to pass each argument in the
> list to a different function.
>
> Thanks,
> Joe
>
>
Use *list_of_args.  Try this:

def sum1( a ):
    return a
def sum2( a,b ):
    return a+b
def sum3( a,b,c ):
    return a+b+c
def sum4( a,b,c,d ):
    return a+b+c+d

def sumOf(list_of_args):
    sumFn = (None, sum1, sum2, sum3, sum4)[len(list_of_args)]
    return sumFn(*list_of_args)  # <-- pass thru args to other function

print sumOf( [ 1,2 ] )
print sumOf( [ 1,2,3 ] )
print sumOf( [ 1,2,3,4 ] )


-- Paul





More information about the Python-list mailing list