[Python-ideas] exception based conditional expression, similar to if-else conditional expression

Steven D'Aprano steve at pearwood.info
Sat Aug 22 07:32:14 CEST 2009

On Sat, 22 Aug 2009 11:28:04 am Ron Adam wrote:
> Greg Ewing wrote:
> > Steven D'Aprano wrote:
> >> Others have suggested that the colon should be dropped. I dislike
> >> that idea, because there's nothing but whitespace delimiting the
> >> list of exceptions from the except-expression:
> >>
> >
> > I agree. Even if it can be parsed unambiguously, it's
> > going to seem weird and confusing to a human.
> >
> > So far I haven't seen anything I like better than
> >
> >   <expr> except <value> if <exception>
> >
> > despite the fact that it uses the words 'except'
> > and 'if' in a different way than elsewhere.
> Possibly the only way to do this within those constraints 

Which constraints? Are you referring to the fact that any proposed 
syntax must be capable of being unambiguously parsed?

> is a purely syntactic.

I'm not sure I understand. Can you give an example of a proposal which 
*isn't* syntactic?

>      value = (expr1 :exception: expr2)

Are the brackets intended to be part of the syntax, or are they there 
just for grouping? In other words, would this be allowed?

value = function(expr1 :exception: expr2)

Or would we have to write this?

value = function((expr1 :exception: expr2))

> Sense most people (and editors) recognize exceptions names easily,
> there really isn't a strong need to use the key word "except" in an
> expression like this.

The word "except" cues in the reader that they are seeing an 
exception-expression, rather than some sort of data structure like 
{key:value} or (a, tuple).

For the same reason, list comprehensions use "for":

[x+1 for x in alist]

rather than mathematical notation:

{x+1 ∀ x ∈ alist}

(if that doesn't display for you, the first odd character is an upside 
down A, "for all", and the second is a curved E, "element of"). If you 
don't like unicode-based source code, we could replace the "for all" 
and "element of" symbols with colons:

[x+1 : x : alist]

but we don't because the words "for" and "in" give the reader hints as 
to what is being done, which a colon would not.

Words are just symbols, so anytime you have syntax including a keyword, 
you could replace the keyword with a symbol. That way leads to APL. As 
a general rule, keywords are more readable, punctuation is more 

Steven D'Aprano

More information about the Python-ideas mailing list