[Tutor] max(len(item)) for cols
jfouhy@paradise.net.nz
jfouhy at paradise.net.nz
Mon Jun 20 11:01:34 CEST 2005
Quoting János Juhász <janos.juhasz at VELUX.com>:
> That I don't know is the asterisk in zip(*[labels] + rows)
> I wasn't able to find in the python reference what it means.
> May you help me in that ?
You can find it in the language reference: http://docs.python.org/ref/calls.html
Basically, suppose you have a list:
>>> myList = ['foo', 42, None]
and a function:
>>> def doSomething(s, n, t):
... print 'The meaning of %s is %d' % (s, n)
... if t:
... return True
... return False
...
Then you can call like this:
>>> doSomething(*myList)
The meaning of foo is 42
False
The *myList means "expand myList into a bunch of positional parameters". So,
doSomething got the first element of myList as s, the second element as n, and
the third element as t.
You can do the same thing in reverse, in the function definition. eg:
>>> def myFunction(*args):
... for x in args:
... print x,
... print
...
>>> myFunction(1, 2, 'foo', 'bar', 37)
1 2 foo bar 37
In this case, all the positional arguments got combined into a tuple, and given
the name 'args'.
Finally, this works with keyword arguments as well, but you have to use
dictionaries, and you use two asterisks instead of one.
>>> myDict = { 'x':3, 'y':7, 'z':19 }
>>> def doSomeArithmetic(y=0, z=0, x=0):
... return y*z+x
...
>>> doSomeArithmetic(**myDict)
136
HTH!
--
John.
More information about the Tutor
mailing list