replacing `else` with `then` in `for` and `try`

Chris Angelico rosuav at gmail.com
Fri Nov 3 07:03:58 EDT 2017


On Fri, Nov 3, 2017 at 8:48 PM, Alexey Muranov <alexey.muranov at gmail.com> wrote:
> 'Then' describes what happens next indeed, unless some extraordinary
> situation prevents it from happening, for example:
>
>    try:
>        go_to_the_bakery()
>    then:
>        buy_croissants(2)
>    except BakeryClosed:
>        go_to_the_grociery()
>        buy_baguette(1)
>    finally:
>        come_back()
>
> I know this is a poor program example (why not to use a boolean return value
> instead of an exception, etc.), and i know that currently in Python `except`
> must precede `else`, it is just to illustrate the choice of terms.

What is the semantic difference between that code and the same without
the "then:"? The normal behaviour of both Python code and human
instruction sheets is to proceed to the next instruction if nothing
went wrong:

1. Go to the office.
2. Sit down at the desk.
3. Open the third drawer down at your right hand.
4. Take the secret instructions.
5. Read and follow the instructions.
-- if something goes wrong, take out your pistol and kill every zombie
you find --

If there's no desk in the office, you won't continue to the next
steps. But for the normal case, you don't have to say "then".

ChrisA



More information about the Python-list mailing list