On 2022-06-20 15:05, Chris Angelico wrote:
On Mon, 20 Jun 2022 at 21:11, Jonathan Fine <jfine2...@gmail.com> wrote:

Hi

Some have liked adding a new syntax
    a, b, ... = iterable
to mean consume two items from the iterable. However,
   a, b, Ellipsis = iterable
has a different meaning (at least in Python 3.8). It redefines Ellipsis. (As an 
explicit constant, '...' can be redefined.)

To clarify: The syntactic token '...' will always refer to the special
object Ellipsis (at least back as far as Python 3.4 - can't remember
when it became available in all contexts), but the name Ellipsis can
be rebound. So even though, in many contexts, "x = Ellipsis" and "x =
..." will have the same net effect, they are distinct (one is a name
lookup and the other is a constant), and they're definitely different
in assignment.

(Though it wouldn't surprise me if a future Python release adds
Ellipsis to the set of non-assignable names, with None/True/False.)

The syntax
  a, b, ... = iterable
so to speak fills a gap in existing syntax, as the construct is at present 
invalid. I actually like gaps in syntax, for the same reason that I like a 
central reservation in a highway. The same goes for the hard shoulder / 
breakdown lane.

The title of this thread includes the phrase 'Stop Iterating' (capitals added). 
This suggests the syntax
  a, b, StopIterating = iterable
where StopIterating is a new keyword that can be used only in this context.

I'd like to know what others think about this suggestion.


Hard no. That is currently-legal syntax, and it's also clunky. I'd
much rather the "assign to ..." notation than a weird new soft keyword
that people are going to think is a typo for StopIteration.

It's worth noting that the proposed syntax has a slight distinction
from the normal asterisk notation, in that it makes perfect sense to
write this:

a, *_, b = thing

but does not make sense to write this:

a, ..., b = thing

as the "don't iterate over this thing" concept doesn't work here.
(Supporting this would require some way to reverse the iterator, and
that's not a language guarantee.)

It could be taken to mean "consume but discard", leaving 'a' bound to the first item and 'b' bound to the last item, but then:

a, ... = thing

would have to leave 'a' bound to the first item and the iterator exhausted.

In fact, use of ... would always have to exhaust the iterator, which, I think, would not be very useful.

Best not to go that way.
_______________________________________________
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/5NXRQSQVZWLIOU64E3CHH4H57VATAOLU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to