[Python-Dev] Extending tuple unpacking

Paul Du Bois paul.dubois at gmail.com
Mon Oct 10 22:14:30 CEST 2005

On 10/10/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
>    cmd, *args = input.split()

These examples also have a reasonable implementation using list.pop(),
albeit one that requires more typing.  On the plus side, it does not violate
DRY and is explicit about the error cases.

  args = input.split()
    cmd = input.pop(0)
  except IndexError:
    cmd = ''

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

  rest = args    # or args[:] if you really did want a copy
    arg1 = rest.pop(0)
    arg2 = rest.pop(0)
  except IndexError:
    raise TypeError("foo() takes at least 2 arguments")


More information about the Python-Dev mailing list