[Python-Dev] Adding a conditional expression in Py3.0
Antoine Pitrou
solipsis at pitrou.net
Thu Sep 29 17:01:30 CEST 2005
> The only problem is that it's not easy to come up with a regex-based
> way to transform
>
> C and X or Y
>
> into
>
> X if C else Y
(my 2 cents)
I find this proposal very confusing. The order is not logical at all.
One usually expects to find the condition on one side, and the
alternatives on another side (this is how it's done in every conditional
construct I know of : traditional if-then-else, lisp's cond, switch
statements...). But there the condition is in the middle, which breaks
the natural reading order and feels obfuscated.
This is especially true if the "X" in "X if C else Y" happens to be a
non-trivial expression - witness your example from unittest.py:
return doc.split("\n")[0].strip() if doc else None
... because then the condition (which is the most important part of the
statement) is shadowed by the complexity of the first alternative; and
the two alternatives, which should logically be siblings, are separated
by something which has a different role in the construct.
This is exactly like a switch/case statement where the "switch" would
have to be inserted in the middle of two "case"'s.
Also, generally, one of the most annoying things in computer languages
is when they try to invent their own unnatural conditional forms: such
as Perl's inverted forms or "unless" statement.
Regards
Antoine.
More information about the Python-Dev
mailing list