[Tutor] "*" and "**"

Chris Readle patterner@rocketmail.com
Tue Jul 8 14:31:10 2003


One little gotcha here that I've run into is that the positional args have
to come before the keyword args.  So the function call as Gregor wrote it:

fun(1, "be", [1,2,3], now=1, more="yep", then={1:"a",2:"be"})

would work, but something like:

fun(more="yep", 1, "be", [1,2,3], now=1, then={1:"a",2:"be"})

would not.

chris

--- Gregor Lingl <glingl@aon.at> wrote:
> Luiz Siqueira Neto schrieb:
> 
> > What this "*" and "**" mean on python in this situation:
> >
> > ---
> > def __init__(*args, **ar)
> > ---
> 
> 
> This means, that you can pass an arbitrary number of positional
> arguments to the function/method, which are collected in a tuple
> named args ...
> ... and an arbitrary number of keyword-arguments, which are collected
> in a dictionary called ar, or usually kwargs, as in the following
> example:
> 
>  >>> def fun(*args, **kwargs):
>     print "args:", args
>     print "kwargs:", kwargs
> 
>    
>  >>> fun(1, "be", [1,2,3], now=1, more="yep", then={1:"a",2:"be"})
> args: (1, 'be', [1, 2, 3])
> kwargs: {'then': {1: 'a', 2: 'be'}, 'now': 1, 'more': 'yep'}
>  >>>
> 
> hth
> Gregor
> 
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> 
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com