For review: PEP 308 - If-then-else expression
Erik Max Francis
max at alcyone.com
Sat Feb 8 21:52:41 CET 2003
> Why is
> when <cond> then <exp1> else <exp2>
> <expression1> if <condition> else <expression2>
> different or better than:
> def when(cond, exp1, exp2):
> if cond:
> return exp1
> return exp2
> and making 'when' built-in??
One word: short circuiting.
And before you go there, making a special form that looks like a
function call but actually does short circuiting and conditional
evaluation of is parameters would set a horrible precedent.
> And if nobody has a regular habit of adding
> this function to every module they write, why
> is the need for a ternary operator pressing?
I think you've missed a lot of the context of this PEP coming up.
People new to Python routinely ask for a conditional expression in
Python. The FAQ even has an entry devoted to it. The fact is, to get
proper conditional expression functionality (replete with short
circuiting), you need to resort to extremely ugly hacks that defeat the
whole purpose of wanting a conditional operator (namely, readability and
conciseness). There are some slightly less ugly idioms that are not
very readable, and will only work in special cases (where the "then"
expression is not false, or when the condition is an actual Boolean),
but all of them are compromises.
The fact is, a lot of people want this functionality, and many have
wanted it for a long time. Is it a majority of people on
comp.lang.python? I don't know. Is it a majority of Python users in
general? No idea. But some do, apparently many. The fact that there
is a FAQ entry devoted to the question is a testament to that.
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Lawyers, I suppose, were children once.
\__/ Charles Lamb
The laws list / http://www.alcyone.com/max/physics/laws/
Laws, rules, principles, effects, paradoxes, etc. in physics.
More information about the Python-list