function arguments

Discussion in 'Python' started by Joe Laughlin, Sep 15, 2004.

  1. Joe Laughlin

    Joe Laughlin Guest

    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
    Joe Laughlin, Sep 15, 2004
    #1
    1. Advertising

  2. Joe Laughlin

    Paul McGuire Guest

    "Joe Laughlin" <> wrote in message
    news:...
    > 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
    Paul McGuire, Sep 15, 2004
    #2
    1. Advertising

  3. Joe Laughlin

    Larry Bates Guest

    Paul has explanation (see his response), but I
    wanted to expand a little.

    def foo(*args, **kwargs):
    for arg in args:
    print arg
    for key, value in kwargs.items():
    print key, value
    return

    >>> foo('a','b',x=1,y=2)

    a
    b
    y 2
    x 1

    or

    def foo(list_of_args, **kwargs):
    #
    # Now call function with args as individual arguments
    # and keyword arguments as individual keyword
    # arguments also.
    #
    call_other_function(*list_of_args, **kwargs)
    return

    Hope this helps,
    Larry Bates


    "Joe Laughlin" <> wrote in message
    news:...
    >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
    >
    >
    Larry Bates, Sep 15, 2004
    #3
  4. Joe Laughlin

    Paul Rubin Guest

    "Joe Laughlin" <> writes:
    > 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 the old days you'd have had to say

    apply(call_other_function, list_of_args)

    But now you can just say

    call_other_function(*list_of_args)

    which does the same thing. The asterisk turns your list into an arglist.
    Paul Rubin, Sep 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    635
    sushant
    Jan 20, 2005
  2. tutmann
    Replies:
    4
    Views:
    421
  3. jmborr
    Replies:
    1
    Views:
    388
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    795
  5. Navaneeth
    Replies:
    4
    Views:
    535
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page