[Python-ideas] except expression

Steven D'Aprano steve at pearwood.info
Sat Feb 15 20:57:31 CET 2014


On Sat, Feb 15, 2014 at 08:24:09PM +0100, Philipp A. wrote:
> hmm i still don’t like the colon. everywhere else in python, it means “new
> block follows”

That is incorrect. Did you read my full post? I specifically mentioned 
both dicts and lambda, both of which use colons in expressions, and 
don't start a new block.


> and i’m sure nobody here wants to allow the following?
> 
> value = hovercraft() except EelsException:
>    remove_eels()
>    no_eels  #this is assigned to “value” if we have too many eels

That's not the suggested syntax.

To start with, if you want to spread the expression over multiple lines, 
you need either line continuation backslashes, or round brackets:

value = hovercraft() (except EelsException:
    remove_eels()
    )


Secondly, you then place an unexpected "no_eels" statement after the 
expression. That will be a syntax error.


> i like it in coffeescript and scala, but it’s not used in python, so we
> shouldn’t introduce it.

What is used in coffeescript and scala?


> neither should we introduce a colon that *can’t* be followed by a block.

Dicts, lambda.


> if we wanted the colon in any case, we’d need to do:
> 
> winner = germans_win() except EurekaException: return greeks

Certainly not. Do we write this?

mydict = {1: return 'a', 2: return 'b'}


> value = hovercraft() except EelsException:
>    remove_eels()
>    return no_eels

This is not the suggested syntax. I suggest you read my post again, and 
notice that this is an *expression*. That is the whole point of the 
thread! If you want a block made up of multiple statements, use a 
try...except statement.


> but i’m still partial to stomach_content = eat() except ThinMintError pass
> explode()

"pass" is a placeholder null statement, it has no relevance to 
try...except. You might just as well sensibly say

stomach_content = eat() except ThinMintError import explode()
stomach_content = eat() except ThinMintError del explode()
stomach_content = eat() except ThinMintError class explode()
stomach_content = eat() except ThinMintError def explode()
stomach_content = eat() except ThinMintError pass explode()

In all of these cases, I have picked a random keyword and just tossed it 
into the expression. None of them make any sense.


-- 
Steven


More information about the Python-ideas mailing list