[Python-Dev] Extending tuple unpacking (original) (raw)

Guido van Rossum guido at python.org
Mon Oct 10 16:50:02 CEST 2005


On 10/10/05, Nick Coghlan <ncoghlan at gmail.com> wrote:

It also works for situations where "the first n items are mandatory, the rest are optional". This usage was brought up in the context of a basic line interpreter:

cmd, *args = input.split()

That's a really poor example though. You really don't want a line interpreter to bomb if the line is empty!

Another usage is to have a Python function which doesn't support keywords for its positional arguments (to avoid namespace clashes in the keyword dict), but can still unpack the mandatory arguments easily:

def func(*args, **kwds): arg1, arg2, *rest = args # Unpack the positional arguments

Again, I'd be more comfortable if this was preceded by a check for len(args) >= 2.

I should add that I'm just -0 on this. I think proponents ought to find better motivating examples that aren't made-up.

Perhaps Raymond's requirement would help -- find places in the standard library where this would make code more readable/maintainable.

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list