|September 29th, 2009|
|python, programming [html]|
5.3.4:This is very handy, but is also limited. In python 3.0, PEP 3132 added some related syntax:
If the syntax *expression appears in the function call, expression must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments; if there are positional arguments x1,..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM
If the * is present [in the function definition argument list], it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple
first, *rest = seqThis is again, handy but limited. This star syntax only works in these two cases: functions (star-args) and assignment targets. There are logical extensions that make a lot of sense to me, though. If one can do:
a, *b, c = range(5)
first, *rest = seqthen I want to be able to do:
seq = first, *restWhile the following is ambiguous and so clearly needs to be illegal:
first, *some, *rest = seqthis is not ambiguous:
seq = first, *some, *restWhile we're at it, lets make the following legal:
foo(first, *some, *rest)All these changes together would make for a very consistent and powerful interpretation of * as 'expand this sequence here'.
Update 2013-04-05: Danner points out a 2008 patch. This was put on hold because of the moratotium (to "allow non-CPython implementations to 'catch up'") but with 3.3 out I think the moratorium is over. I just poked the bug.