[Tutor] my problem with **

Kristoffer Erlandsson krier115@student.liu.se
Tue Apr 29 04:30:07 2003


On Wed, Apr 30, 2003 at 04:16:48AM +0800, ali mangaliag wrote:
> what does the ** and * mean in a function parameter??
> 
> ex...
> 
> def foo(*bar, **kwargs):
>     .....

When using one '*' that argument accepts any excess arguments and wraps them up
in a tuple. When using '**' all excess keyword arguments are wrapped up in
that argument using a dictionary. Using an example to make it clearer:

>>> def foo(*args1, **args2):
...     print args1
...     print args2
... 
>>> foo()
()
{}
>>> foo(1,2,3)
(1, 2, 3)
{}
>>> foo(mip='meep', mop='mooop')
()
{'mip': 'meep', 'mop': 'mooop'}
>>> foo(1,2,3, mip='meep', mop='moop')   
(1, 2, 3)
{'mip': 'meep', 'mop': 'moop'}

If you want you can have a number of ordinary arguments before the ones with
the *:s, these work like usual then and the *:ed ones catch the excess
arguments.

Regards,
Kristoffer



-- 
Kristoffer Erlandsson
E-mail:  krier115@student.liu.se
ICQ#:    378225