(test) ? a:b

Ned Batchelder ned at nedbatchelder.com
Sat Oct 25 13:58:14 CEST 2014


On 10/25/14 1:03 AM, Steven D'Aprano wrote:
> 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]()
>>> >>
> [Dan]
>>> >>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.

You mention "standard Python idioms."  I think this style of 
conditional-via-indexing is becoming quite uncommon, and is no longer 
one of the standard Python idioms.  This is now in the category of 
"outdated hack."

Yes, its meaning is well-defined by the language.  But no, its meaning 
is not immediately apparent to most of today's Python programmers.

Of course, opinions obviously differ.

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list