single-line terinary operators considered harmful
Lennart Regebro
lennart at regebro.nu
Wed Mar 5 09:18:10 EST 2003
"Clark C. Evans" <cce at clarkevans.com> wrote in message news:<mailman.1046832150.1332.python-list at python.org>...
> Getting it to fit on a single line is just an unnecessary
> distraction, and, IMHO, one that is bogging down the whole
> discussion down... and preventing a good mechanism to
> express conditional logic within an expression.
Yup.
That said, I don't mind a x = conditional(c, a, b), becuase it looks
like a function. If people neseecarily have to make if-then-else
statements like this on one line, create that function. Heck, here it
is:
conditional.py:
def conditional(c, a, b):
if c:
return a
else:
return b
Done!
Look, I even tested it for you! :)
>>> from conditional import conditional
>>>
>>> x = conditional(1, 'True', 'False')
>>> print x
True
So there. No, go and vote "I reject all options". There is no point in
adding ugluness to a pretty language if that uglyness doesn't have a
real value, and this hasn't. If you want one line conditionals, now
you have them. :)
More information about the Python-list
mailing list