Inline Conditionals?

Antoon Pardon apardon at forel.vub.ac.be
Thu Aug 26 09:55:59 EDT 2004


Op 2004-08-26, Alex Martelli schreef <aleaxit at yahoo.com>:
> 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.

When using list comprehension not having a ternary operator can be
a PITA. It is of course possible I miss something but how am I
supposed to do the following:

   [ x.property ? foo(x) : bar(x) for x in Somelist ]

-- 
Antoon Pardon



More information about the Python-list mailing list