Inline Conditionals?

Alex Martelli aleaxit at yahoo.com
Thu Aug 26 09:00:37 EDT 2004


Peter Hansen <peter at engcorp.com> wrote:

> Joshua Ginsberg wrote:
> 
> > Is there any plan to include inline conditionals in Python? For example:
> > 
> > def isNegative(x):
> >     return x < 0 ? True : False
> 
> This is a FAQ: 
> 
> Newbies, please consider reading the several FAQs that you will find
> at http://www.python.org/doc/faq/ before posting questions which
> might be answered there (i.e. just about anything).

Peter's right.  And Joshua's example shows how wise the BDFL was in
ruling out ternaries: sure, good programmers might occasionally have
found good uses for them, but we' have paid that with a LOT of horrid
code like that -- I've seen lots like that in C & its ilk, too.

    return x < 0

on its own does EXACTLY the same job as the requested/wished-for

    return x < 0 ? True : False

except is cleaner, clearer, faster, more concise, easier to read.  So,
having a ternary operator would enable a little programming horror that
the lack of a ternary operator dissuades a little.

Of course, absit iniuria verbis, one cannot make anything foolproof,
because fools are SO ingenious -- I've also seen lots of code like:

    if x < 0:
        return True
    else:
        return False

which is, si licet es, even _worse_ than the wished-for ternary use!-)


Alex



More information about the Python-list mailing list