[Python-Dev] Extending tuple unpacking

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Oct 12 04:17:02 CEST 2005

Steve Holden wrote:

> So presumably you'd need to be able to say
>    ((a, *b), c, *d) = ((1, 2, 3), 4, 5, 6)


>    a, *b = [1]
> to put the empty list into b, or is that an unpacking error?

Empty sequence in b (of whatever type is chosen).

> does this mean that you'd also like to be able to write
>    def foo((a, *b), *c):

That would follow, yes.

> I do feel that for Python 3 it might be better to make a clean 
> separation between keywords and positionals: in other words, of the 
> function definition specifies a keyword argument then a keyword must be 
> used to present it.

But then how would you give a positional arg a default value
without turning it into a keyword arg?

It seems to me that the suggested extension covers all the
possibilities quite nicely. You can have named positional args
with or without default values, optional extra positional
args with *, named keyword-only args with or without default
values, and unnamed extra keyword-only args with **.

The only thing it doesn't give you directly is mandatory
positional-only args, and you can get that by catching them
with * and unpacking them afterwards. This would actually
synergise nicely with * in tuple unpacking:

   def f(*args):
     a, b, *rest = args

And with one further small extension, you could even get
that into the argument list as well:

   def f(*(a, b, *rest)):

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+

More information about the Python-Dev mailing list