How about reserving unicode numeric superscript characters 0..9 as label identifiers only to be used for loop & break, etc. Then the example below would become:

 

 

while¹ not processed():

    for x in the_stuff as 37:

        if all_done(x):

            break¹

 

Steve Barnes

 

From: David Mertz <mertz@gnosis.cx>
Sent: 05 December 2020 17:32
To: Alexis Masson <a.masson555@ntymail.com>
Cc: python-ideas <python-ideas@python.org>
Subject: [Python-ideas] Re: Make for/while loops nameable.

 

On Sat, Dec 5, 2020 at 11:05 AM Alexis Masson <a.masson555@ntymail.com> wrote:

I agree with you that the point of `break x` — labeled or numbered — is to target a specific loop to "jump" to, but I'm not comfortable with introducing labels to Python. The reason for this is that they look too much like identifiers, and should, therefore, be bound to something, following Python's motto of "everything is an object". I don't see what kind of an object could be bound to that, what kind of API it would expose, how it could be manipulated/re-affected, it behavior out of the loop…

 

I don't really care how the labels are spelled, just that they are spelled on the line that defines the loop.  For example:

 

while not processed() as 1:

    for x in the_stuff as 37:

        if all_done(x):

            break 1

 

Numbers like 1 and 37 are pretty clearly not binding targets.  By convention, using numbers 1, 2, 3, 4 for successive levels might be good.  But it wouldn't break directly if you added or removed a nested loop as long as you kept its number.  That said, allowing slightly more descriptive names than "2" seems sorta nice.  Using sigils to make it "obviously not a binding target" is way too much ugly for the limited need.

 

I don't think this feels Pythonic, but if the labels were exclusively one-digit positive integers, that would limit named loop depth to 9.  That is a good thing.  Anything over 4 levels is questionable, anything over 6 is monstrous.  So that's extra leeway for anything that is actually a good idea.  But I think "use a natural number" and rely on "we're all adults here" is more likely to fit in Python.



--

The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.