[Python-Dev] A minimal Python interpreter written in Python for experimenting with language changes

Steven D'Aprano steve at pearwood.info
Mon Feb 5 22:40:13 EST 2018


On Sat, Feb 03, 2018 at 11:45:15AM +0100, asrp wrote:

> > Can you give an example of how you would do that? I don't mean the 
> > mechanism used, I mean how would a developer implement a new syntactic 
> > feature. Suppose I wanted to add a new clause to for...else, let's say:
> >
> >     for ... :
> >        block
> >     otherwise:
> >        # runs only if the for-loop was empty
> > 
> > How would do I do that?
[...]
> If you tell me a bit more about the intended behaviour of "otherwise", 
> I'd be happy to do an example with that clause.


Here's a faked session showing the sort of thing I am referring to. 
(Note that this is just an example, not a proposal for a new language 
feature.)

for x in [1, 2, 3]:
    print(x)
otherwise:
    print("nothing there")


prints 1, 2, 3.

for x in []:
    print(x)
otherwise:
    print("nothing there")

prints "nothing there". In other words, the otherwise block runs if, and 
only if, the loop iterable is empty and the for block does NOT run.

Can you do something like that?



-- 
Steve


More information about the Python-Dev mailing list