[Tutor] "*" and "**"

Gregor Lingl glingl@aon.at
Tue Jul 8 13:18:04 2003


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
>
>