(test) ? a:b

alister alister.nospam.ware at ntlworld.com
Wed Oct 22 14:03:57 EDT 2014


On Thu, 23 Oct 2014 03:28:48 +1100, Steven D'Aprano wrote:

>> Why on earth would you recommend this outdated hack, when there's a
>> true conditional operator?
>> 
>>      j = 3 if j >= 10 else j+1
> 
> I think that's a bit harsh. Especially since this appears to have been
> Buscacio's first post here. Hopefully not his(?) last post!
> 
> The old (b, a)[condition] idiom is not outdated for anyone supporting
> Python 2.4, and I wouldn't call it a hack. Indexing into a sequence with
> a bool is basic to Python's semantics: True and False are ints equal to
> 1 and 0 respectively. It's also a technique easily extensible to more
> than two values:
> 
>     '01TX'[n % 4]
> 
> is in my opinion more readable than:
> 
>     i = n % 4 '0' if i == 0 else '1' if i == 1 else 'T' if i == 3 else
>     'X'

chained ternary operations are evil no mater what style or language they 
are written in as they rapidly become confusing & unreadable

"Readability counts"

in my opinion they are better written as nested if statements 

-- 
Ambiguity:
	Telling the truth when you don't mean to.



More information about the Python-list mailing list