[Python-Dev] Adding a conditional expression in Py3.0

Guido van Rossum guido at python.org
Tue Sep 20 23:40:45 CEST 2005

(Adding python-dev back to the CC list)

On 9/20/05, Jason Orendorff <jason.orendorff at gmail.com> wrote:
> > If there's one thing I've learned from the PEP 308 vote, it is that
> > votes for language don't work. I prefer some discussion on Python-dev
> > after which I pick one.
> +1
> Some visual aids:
>     return (if q: q.popleft() else: None)
>     return (if q then q.popleft() else None)
>     return q ? q.popleft() : None
> Hmmm.  Score one for ?:.  But:

Why? Just because it's shorter?

>     menu.append(
>         if gotHerring(): popHerring()
>         elif gotAnyFish(): popAnyFish()
>         else: Tofurbot())
>     menu.append(gotHerring() ? popHerring() : gotAnyFish() ?
> popAnyFish() : Tofurbot())
> Here, I like the way the verbose syntax politely spreads itself out
> over multiple lines.  In C, I never know where to put the line breaks.

Ouch. You're bringing up another valid issue: whether to support
"elif". I think if we go with (if ... then ... else ...) or (if ...:
... else: ...) we'll have to support elif as well:

(if ... then ... elif ... then ... else ...)
(if ...: ... elif ...: ... else: ...)

I really don't like the latter.

Here's a suggestion for a way to decide between a "wordy" version or
C-style "?:" -- if we abandon and/or in favor of &&/||, we should also
go with ?:; if we keep and/or, we should use a keyword-based
conditional as well. Since so far the feedback is overwhelmingly in
favor of keeping and/or, I think that settles the case in favor of a
wordy version. My preference then would be

(if ... then ... elif ... then ... else ...)

which gives my a nice nostalgic feeling because (except for the elif
part) Algol-60 had the same thing -- the first programming language I
ever learned. :)

(Oh, and a way to decide between colon or no colon: we're not using
colons in list comps and genexprs either.)

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

More information about the Python-Dev mailing list