[Python-Dev] conditional expressions?

Skip Montanaro skip@pobox.com (Skip Montanaro)
Mon, 15 Oct 2001 15:13:48 -0500


I misunderstood about the need for parens.  I had only been watching the
thread in python-list with one eye.  I thought it was just some python-list
riff-raff throwing around ideas.  Then I saw that Tim was part of the rabble
;-).  I still didn't notice the "I've implemented it but not yet checked it
in" bit until someone else did and asked about it.  I then went looking for
a PEP but didn't see one.  I hadn't seen any discussion on python-dev
either.  (Maybe I missed it because python-list and python-dev mail wind up
in the same mailbox.)

    Guido> If I saw a piece of code that read

    Guido>     x = if if if x == 1 then y else z then p else q then a else b

    Guido> I would get a strong urge to commit illegal violence against the
    Guido> author.  If on the other hand I saw

    Guido>     x = if (if (if x == 1 then y else z) then p else q) then a else b

    Guido> I might be willing to sit down and figure out what it meant --
    Guido> maybe with the help of a parentheses-balancing command in my
    Guido> editor.

Python's block structured statement syntax keep you from composing just
monstrosities at the statement level.  I'm not sure what makes them more
appealing at the expression level.  How many levels of parens would be
required before you had a "strong urge to commit illegal violence" against
someone using a parenthesized conditional expression?

I guess that's what it boils down to for me.  You can easily write much more
inscrutable code with the new conditional expression than you could before.
I think that will make Python somewhat less of a CP4E language.  I don't
think it falls into the same category of the more esoteric features of the
language (generators, metaclasses, __*__ methods, etc) that beginners can
safely ignore.  Conditional expressions are pretty basic stuff, and this
makes it easier to write fairly inscrutable conditional expressions.

    Guido> Sorry, I still don't get this at all.  A conditional expression
    Guido> is still an expression.  Curly brances are a statement-level
    Guido> concept.  What am I missing?  Where is the similarity between the
    Guido> use of { } and ( ) in C/Java/C++/Perl?

>From the computer's viewpoint, they are syntactically and semantically,
different, I agree.  From the point of view of a human trying to understand
what's written I think they will serve much the same purpose, however.
White space is viewed is being essential enough to understanding that Python
uses it for block structure.  In particular, I can't nest two compound
statements on the same line.  With this new addition to expressions, it
appears you will be able to create arbitrarily complex expressions all on
one line.

Skip