Passing variable number of named arguments

Discussion in 'Python' started by Ramashish Baranwal, Dec 27, 2006.

  1. Hi,

    I need to process few out of a variable number of named arguments in a
    function and pass the remaining to another function that also takes
    variable number of named arguments. Consider this simple example,

    def fun1(**kwargs):
    print kwargs.keys()

    def fun2(**kwargs):
    # get id param
    id = kwargs.pop('id', '')
    # pass on remaining to fun1
    fun1(kwargs)

    When I try to call fun2 I get the following error-

    TypeError: fun1() takes exactly 0 arguments (1 given)

    It seems that the arguments are not passed to fun1 as named arguments.
    How can I go about this? Using a dictionary in place of kwargs would be
    a way, but I can't modify fun1, so thats ruled out for me.

    Thanks,
    Ram
    Ramashish Baranwal, Dec 27, 2006
    #1
    1. Advertising

  2. On Wed, 2006-12-27 at 10:37 -0800, Ramashish Baranwal wrote:
    >[...]
    > def fun2(**kwargs):
    > # get id param
    > id = kwargs.pop('id', '')
    > # pass on remaining to fun1
    > fun1(kwargs)
    >
    > When I try to call fun2 I get the following error-
    >
    > TypeError: fun1() takes exactly 0 arguments (1 given)
    >
    > It seems that the arguments are not passed to fun1 as named arguments.


    You have to call fun1 like this: fun1(**kwargs).

    -Carsten
    Carsten Haese, Dec 27, 2006
    #2
    1. Advertising

  3. Carsten Haese wrote:
    > On Wed, 2006-12-27 at 10:37 -0800, Ramashish Baranwal wrote:
    > >[...]
    > > def fun2(**kwargs):
    > > # get id param
    > > id = kwargs.pop('id', '')
    > > # pass on remaining to fun1
    > > fun1(kwargs)
    > >
    > > When I try to call fun2 I get the following error-
    > >
    > > TypeError: fun1() takes exactly 0 arguments (1 given)
    > >
    > > It seems that the arguments are not passed to fun1 as named arguments.

    >
    > You have to call fun1 like this: fun1(**kwargs).


    Wow. thanks Carsten..

    -Ram
    Ramashish Baranwal, Dec 28, 2006
    #3
    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. Replies:
    5
    Views:
    506
  2. Replies:
    3
    Views:
    801
  3. Navaneeth
    Replies:
    4
    Views:
    540
    Kenny McCormack
    Nov 20, 2010
  4. Roger Pack
    Replies:
    2
    Views:
    101
    Roger Pack
    Jan 4, 2010
  5. oldyork90
    Replies:
    10
    Views:
    335
    Jorge
    Sep 27, 2008
Loading...

Share This Page