[Python-ideas] Break multiple loop levels
haael
haael at interia.pl
Mon May 13 09:41:40 EDT 2019
How about doing it pythonic way then, "everything is a hack on a symbol
table".
Let normal loops remain normal loops.
Let's introduce a special construction:
> for x in iterator as loop_control_object:
> loop_body(loop_control_object)
The iterator in the loop would be wrapped inside another iterator that
would yield everything as the original iterator, but it would also be
controllable through loop_control_object.
Sample implementation:
> def make_control_object(iterator):
> control_object = ControlObject()
> control_object.running = True
> def breakable_iterator():
> for x in iterator:
> yield x
> if not control_object.running:
> return
> return breakable_iterator(), control_object
The control object could have methods that allow sophisticated loop
control, such as breaking, continuing, injecting exceptions, counting
iterations, skipping etc.
The keywords 'break loop_control_object' and 'continue
loop_control_object' could be simply implemented as:
> loop_control_object.do_break()
> On Sun, May 12, 2019, 5:36 PM Paul Moore <p.f.moore at gmail.com
> <mailto:p.f.moore at gmail.com>> wrote:
>
> On Sun, 12 May 2019 at 21:06, David Mertz <mertz at gnosis.cx
> <mailto:mertz at gnosis.cx>> wrote:
> > I thought of 'as' initially, and it reads well as English. But it
> felt to me like the meaning was too different from the other
> meanings of 'as' in Python. I might be persuaded otherwise.
>
> If you think in terms of "named loops" rather than "labels", "as"
> makes a lot more sense.
>
>
> Every name created with 'as' currently is a regular Python object, with
> a memory address, and some methods, that can participate in comparison
> (at least equality), that has a type, can be passed as a function
> argument, etc. Actually, all that is true of EVERY name in Python other
> than keywords.
>
> It seems like none of this would be true of a loop name/label. Hence my
> first thought that a different way of introducing the word is more
> clear... Maybe we can just prefix lines with labels instead, and add a
> 'goto' command :-)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
More information about the Python-ideas
mailing list