What's the business with the asterisk?

Kay Schluehr kay.schluehr at gmx.net
Sat Jan 24 14:49:16 CET 2009


On 24 Jan., 13:31, mk <mrk... at gmail.com> wrote:
> Hello everyone,
>
>  From time to time I spot an asterisk (*) used in the Python code
> _outside_ the usual *args or **kwargs application.
>
> E.g. here:http://www.norvig.com/python-lisp.html
>
> def transpose (m):
>    return zip(*m)
>  >>> transpose([[1,2,3], [4,5,6]])
> [(1, 4), (2, 5), (3, 6)]
>
> What does *m mean in this example and how does it do the magic here?
>
> Regards,
> mk

If zip is specified as

def zip(*args):
    ...

one can pass zero or more arguments into zip. In the zip body one has
access to the argument tuple args. So zip(a, b, c) yields args = (a,
b, c). Now suppose you want to pass the tuple t = (a, b, c) to zip. If
you call zip(t) then args = ((a, b, c),). When calling zip(*t) instead
the tuple is passed as variable arguments just like they are specified
in the signature of zip. So args = (a, b, c).

Same holds for

def foo(**kwd):
    ...

and foo(**kwd) versus foo(kwd).



More information about the Python-list mailing list