Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nope, nothing wrong with it.<br> It's very pythonic in fact. I recall reading that that's the preferred (by Guido himself) ternary-equivalent. I'll see if I can dig up the reference.<br> Personally, I find the other way more readable, but you didn't ask for my personal opinon. ;)</p> <p>UPDATE: Here's the citation. Core Python Programming, 2nd ed.; Wesley J. Hun; Prentice Hall 2007</p> <blockquote> <p>If you are coming from the C/C++ or Java world, it is difficult to ignore or get over the fact that Python has not had a conditional or ternary operator (C ? X : Y) for the longest time. [...] Guido has resisted adding such a feature to Python because of his belief in keeping code simple and not giving programmers easy ways to obfuscate their code. However, after more than a decade, he has given in, mostly because of the error-prone ways in which people have tried to simulate it using and and or - many times incorrectly. According to the FAQ, the one way of getting it right is (C and [X] or [Y])[0]. The only problem was that the community could not agree on the syntax. (You really have to take a look at PEP 308 to see all the different proposals.) This is one of the areas of Python in which people have expressed strong feelings. The final decision came down to Guido choosing the most favored (and his most favorite) of all the choices, then applying it to various modules in the standard library. According to the PEP, "this review approximates a sampling of real-world use cases, across a variety of applications, written by a number of programmers with diverse backgrounds." And this is the syntax that was finally chosen for integration into Python 2.5: X if C else Y.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload