(test) ? a:b

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Oct 25 07:03:16 CEST 2014

alister wrote:

> On Fri, 24 Oct 2014 10:20:30 -0700, Dan Stromberg wrote:
>> On Fri, Oct 24, 2014 at 1:38 AM, Steven D'Aprano
>> <steve+comp.lang.python at pearwood.info> wrote:
>>> I don't get why that's considered hard to read.
>>> So why is it hard to read when the index is a flag?
>>> value = [f, g][cond]()
>> It's clear to you, it's clear to me, but is it clear to everyone?  I
>> very much doubt it.

Of course it won't be clear to *everyone* but it should be clear enough to
people who are familiar with standard Python idioms. A concrete example
should be more obvious than the fake example:

title = ('Mr', 'Ms')[person.sex == 'F']

which should be clear to anyone who understands indexing in Python and that
True == 1 and False == 0.

Although that's probably better written as a dict lookup:

title = {'M': 'Mr', 'F': 'Ms'}[person.sex]

which is then more easily extended to support intersex and
non-traditional[1] gender identities. Better still is to allow the
individual to choose their own title (Dr, Prof, Mrs, Miss, Mx, Sir, Dame,
etc.) rather than calculate it from their sex, but that's moving away from
the point I am making that this idiom isn't some weird and hackish
bizarrity. We use list indexing and key lookup all the time, this is just a
special case of the same. It's no more hackish than:

mapping = {True: "something which is factual",
           False: "something which people prefer to believe"}
value = mapping[flag]

> I had to mentally step through this before it became apparent what it was
> doing,  can see places where it could be usefull (a switch replacement)
> but it is not instantly obvious

Very little code is instantly obvious. Even when you know what the syntax
means, you still have to understand the semantics, and sometimes that's far
from obvious.

> a = <value> if <condition> else <another Value>
> is instantly obvious (at least to a native English speaker anyway)

Ha! And yet people have, and continue to, complain *bitterly* about the
non-standard ordering of Python's ternary if, compared to C, standard
if...else syntax, and English.

"If the syntax is like C, then people will use it, or else they will
complain that the syntax is incomprehensible."

Personally, I don't accept or agree with such complaints. Python's ternary
if follows the same syntax as this English variant:

"People will use it if the syntax is like C, or else they will complain that
the syntax is incomprehensible."

Still, English-like though it is, Python's ternary if does violate the
expectations of those who expect the condition to come first, as the
standard if...else block does.

>> Also, you've gone to the trouble of def'ing two functions here - you may
>> as well do a function for the if/else.

Not every one-liner expression needs to go into a function.

[1] For Western Christian definitions of traditional. Other cultures have
traditionally recognised more than just a binary state of gender and
sexuality, e.g. https://en.wikipedia.org/wiki/Hijra_%28South_Asia%29


More information about the Python-list mailing list