[Python-Dev] conditional expressions?

Guido van Rossum guido@python.org
Mon, 15 Oct 2001 15:34:19 -0400


> Well, the examples I saw looked like
> 
>     x = (if (if e1 then e2 else e3) then e4 else e5)
> 
> Seems to me that those inner parens are there to separate the inner
> conditional from the trailing parts of the outer conditional:
> 
>     x = (
>         if (
>             if (
                 ^
this is not there

>                e1
>             then
>                e2
>             else
>                e3
>             )
>         then
>             e4
>         else
>             e5
>         )
> 
> Whether or not that's what they are in a language parser sense, they sure
> look like it to the human eye.

They are syntactically unnecessary; they are mostly for guidance of
the human reader.  If I saw a piece of code that read

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

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

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

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

>     Tim> I'm not concerned about 21 bad arguments versus 20 <wink>.
> 
> That 21st argument will be a little stronger than the other, because
> it will go something like, "Why can't we have delimiters for block
> statements?  After all, we have them in conditional expressions."
> The reference, instead of being to another language, will be to
> Python itself.

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

--Guido van Rossum (home page: http://www.python.org/~guido/)