variable arguments question

Swaroop C H g2swaroop at yahoo.com
Tue Mar 15 02:52:54 EST 2005


--- vegetax <vegeta.z at gmail.com> wrote:
> how can i pass it to a generic function that takes variable
> keywords as
> arguments? same thing with variable arguments, i need to pass a
> list of
> arguments to the function
> 
> def asd(**kw): print kw
> def efg(*arg): print arg
> 
> asd(d) 
> doesnt work
> asd(kw = d) 
> doesnt work

You need to use it as follows:

    >>> def foo(**args):
    ...     '''Takes in key=value parameters.'''
    ...     for key, value in args.items():
    ...             print key, '->', value
    ...
    >>> foo(a=1,b=2)
    a -> 1
    b -> 2

Hope this makes some sense,

Swaroop C H
Blog: http://www.swaroopch.info
Book: http://www.byteofpython.info



More information about the Python-list mailing list