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

Alexey Muranov alexey.muranov at gmail.com
Fri Nov 3 12:15:08 EDT 2017


On Fri, 2017-11-03 at 22:03 +1100, Chris Angelico wrote:
 > 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:"?

Chris,

the semantic difference is that their meanings/behaviours are not 
identical (i imply that `then` here does what `else` currently does).  
I agree however that from practical viewpoint the difference will 
probably never be observable (unless the person enters the bakery and 
asks for croissants, but during this time the baker exits the bakery 
and closes it to go on vacation).

I can try to think of a better example if you give me any good use-case 
for `else` in `try`.  I have searched on-line, and on StackOverflow in 
particular, but didn't find anything better that this:

  * https://stackoverflow.com/a/6051978

People seem very shy when it comes to giving a real-life example of 
`else` in `try`.

Alexey.




More information about the Python-list mailing list