On Sun, Jun 19, 2022 at 1:01 PM Chris Angelico <ros...@gmail.com> wrote:

> On Mon, 20 Jun 2022 at 05:32, Mike Miller <python-id...@mgmiller.net>
> wrote:
> >
> > My first thought was next(), which I use occasionally:
> >
>
> It's fine for exactly two elements, where you'll never need to adjust
> the code to want three, and where you know already that this is an
> iterator (not some other iterable). If you had five elements to
> unpack, it would be quite clunky, and even more so if you wanted to
> change the precise number of elements unpacked, as you'd have to match
> the number of next calls.
>
What if next grew a new argument? Changing the signature of a builtin is a
big change, but surely not bigger than new syntax? If we could ask for the
number  of items returned the original example might look like

>>> first, second = next(iter(items), count=2)

I don’t think anyone who has started to learn python would be confused by
this. And this new arg could be combined with the existing default to avoid
possible exceptions.

>>> spam, eggs, cheese = next(range(1), 9, count=3)
>>> spam, eggs, cheese
… 0, 9, 9

I guess this is starting to look like the islice solution, but now it’s
magically in the builtin namespace. I don’t recall ever using islice
myself, but I would believe the one argument form to be the most commonly
used.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PRSY3NYT4Z74DNETVSCH37SXDNELIZ7N/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to