[Python-Dev] Extending tuple unpacking
Nick Coghlan
ncoghlan at gmail.com
Mon Oct 10 11:21:56 CEST 2005
Fred L. Drake, Jr. wrote:
> On Sunday 09 October 2005 22:44, Greg Ewing wrote:
> > I'm aware of the differences, but I still see a strong
> > similarity where this particular feature is concerned.
> > The pattern of thinking is the same: "I want to deal
> > with the first n of these things individually, and the
> > rest collectively."
>
> Well stated. I'm in complete agreement on this matter.
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()
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
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list