Proposed PEP for a Conditional Expression

Don O'Donnell donod at home.com
Mon Sep 17 03:58:43 EDT 2001


Michael Chermside wrote:
...
>Abstract
>
>     A Frequently Asked Question by Python newbies is "How do I do the
>     equivalent of C's ?: syntax?". The answers, while fairly well
>     documented, are imperfect or incomplete. Thus many have suggested
>     adding some syntax to Python for this purpose. The goal of this
>     PEP is to gather all of the commonly proposed options and the
>     arguments both pro and con. It will thus serve as a focal point
>     for discussion and perhaps even be eventually accepted or
>     rejected.
>
...

When I first started programming with Python I was disappointed by the
absence of the C type conditional expression.  However, after having
written some tens of thousands of lines of Python code, there were very
few times when I really felt a need for it, being able to use the
workarounds most of the time.  So to me, this addition to the language
is not a high priority, and I would rather see scarce developer
resources used in other areas.

But, just in case this PEP is approved, and the conditional expression
makes it's way into the language, I would like to contribute my humble
opinion to the syntax arguments.

I cast my vote for SPEC_1.
... 
>      SPEC_1: Use the "C syntax".
>          The following production would be added to Python syntax:
>              expression:   expression ?  exprression : expression
>          Since "?" is not currently used in Python, this would never
>          be syntactically ambiguous. Sample: a = b ? c : d
> 
>          PRO - [familiar to programmers]
>          CON - [hard to read] [not pythonic]
...

The arguments against it, [hard to read] and [not pythonic], I believe
are invalid. 

<exp1> ? <exp2> : <exp3>

This expresses the meaning clearly and succinctly (which is a very
Pythonic thing to do) and is very easy to read: 
1.  <exp1>  - evaluate it
2.  ?       - makes it a query, is it true?
3.  <exp2>  - if true, evaluate it
4.  <exp3>  - else, evaluate it

I don't believe the use of special symbols ? and : is any less Pythonic
than the use of other symbols to denote operators which are also
borrowed from the C language.

The use of key words 'if', 'then', 'else' rather than symbols (?:) seems
an unnecessary COBOLization of Python, and would not add anything to
it's readability (except perhaps to a non-programmer).

Following this line of reasoning we should write:
>>> a indexed by i = call f using x, y
rather than:
>>> a[i] = f(x,y)

Exactly why are the symbols [] and () considered Pythonic but ? and :
are not?

My other objection to the alternate syntax suggestions in this PEP and
thread, which all advocate using key words "if", "then", "else" in some
combination or order, is that the use of these words makes it look too
much like a statement rather than an expression.  Expressions should use
simple operators like + - * / % < > <= >= == etc., not key words.  Key
words should signal a statement, like: for, while, print, if, etc. 

Use of the if/then/else keyword syntax would lead to the following
anomalies and possible confusion:

Assuming SPEC_2/SPEC_3:
>>> a = 5
>>> if a > 0 then print "a is positive" \  # invalid conditional expr
    else print "a is not positive"         # continued on second line
SyntaxError: invalid syntax, cond expr can not contain stmt

>>> if a > 0: print "a is positive"    # valid if stmt
    else: print "a is not positive"
a is positive

Or, even more confusing, assuming SPEC_4:
>>> a = 5
>>> if a > 0: print "a is positive" \    # invalid conditional expr
    else: print "a is not positive"      # continued on second line
SyntaxError: invalid syntax, cond expr can not contain stmt

>>> if a > 0: print "a is positive"    # valid if stmt, looks almost
    else: print "a is not positive"    # like the invalid cond above
a is positive

I know that the conditional expression will normally appear imbedded in
some statement, not standing alone as above.  But just the thought of
writing:
>>> a = if ... else ...
seems weird to me.  Anything starting with "if" is a statement to me. 
And that's the way it ought to be.

Just my 2 cents, for what it's worth.

Cheers,
Don



More information about the Python-list mailing list