What's the business with the asterisk?

K

Kay Schluehr

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top