else on the same line - howto
jarausch at remove-igpm.rwth-aachen.de
Thu Oct 16 09:36:13 CEST 2003
Terry Reedy wrote:
> "Helmut Jarausch" <jarausch at remove.igpm.rwth-aachen.de> wrote in
> message news:bmj859$iph$1 at nets3.rz.RWTH-Aachen.DE...
>>Since Python lacks conditional expression
>>k+= (dy >= 0 ? 1 : -1)
> For this specific example:
> k+= (dy >= 0 and 1 or -1)
> But don't do this unless and until you 1) understand why this works
> and why the and branch must be non-null and what happens if it is null
> (this is discussed both in the FAQ and PEP 308) AND 2) accept
> responsibility for always remembering the caution.
Thanks to all who have replied. It's amazing how many different
solutions are possible in Python.
Among all such a solution by a function is most readable (to me)
and I can well imagine such a function a builtin.
I have been teaching course in C++ to beginners and made the experience
that well chosen conditional expression are not obscure.
One can misuse any feature of a language to write obscure code.
Especially for the expression of a 'return' statement a conditional
expression might be a good idea.
So it's a pity that PEP 308 has been rejected since nobody is forced to
use such a construct if he/she doesn't like to.
More information about the Python-list