(a==b) ? 'Yes' : 'No'
ethan at stoneleaf.us
Fri Apr 2 22:54:44 CEST 2010
> In <mailman.1326.1269971785.23598.python-list at python.org> Steve Holden <steve at holdenweb.com> writes:
>> John Nagle wrote:
>>> Chris Rebert wrote:
>>>> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck at hotmail.com>
>>>>> Hi, how can I write the popular C/JAVA syntax in Python?
>>>>> Java example:
>>>>> return (a==b) ? 'Yes' : 'No'
>>>>> My first idea is:
>>>>> return ('No','Yes')[bool(a==b)]
>>>>> Is there a more elegant/common python expression for this?
>>>> Yes, Python has ternary operator-like syntax:
>>>> return ('Yes' if a==b else 'No')
>>>> Note that this requires a recent version of Python.
>>> Who let the dogs in? That's awful syntax.
>> Yes, that's deliberately awful syntax. Guido designed it that way to
>> ensure that people didn't aver-use it, thereby reducing the readability
>> of Python applications.
> Is that for real??? It's the QWERTY rationale all over again. Swell.
The rationale I remember is that it's intended primarily where the
condition is usually true, with the false only being once in a while.
> Second, sticking the test between the two alternatives goes against
> a vast tradition in programming languages. This tradition inevitably
> fosters habits and expectations when reading code, so going against
> it automatically makes code less readable to all who were educated
> in that tradition.
So you're saying that new languages can't change anything already well
established? So much for break-through innovations.
And what about the programmers? It is good to learn to think in
At any rate, I far prefer it over C's syntax.
More information about the Python-list