PEP 308: <condition> then <expression1> else <expression2>
cx at cx.hu
Fri Feb 28 01:06:40 CET 2003
I often need inlined conditions when defining class
attributes to give a default value for something.
Sometimes workarounds must be implemented to
give similar functionality as an inlined conditional.
A conditional function is used to implement this,
but it has hard to debug side-effects and severally
decreases application efficiency in some cases.
I had to implement classes or use ugly lambdas
as a workaround, but they are not an elegant solution.
I've read PEP 308 and found the
condition then 'true' else 'false'
syntax very simple and elegant.
Let's analyse the possibility of ambiguous syntax:
c1 then (c2 then t2 else f2) else (c3 then t3 else f3)
can be written without ambiguity as:
c1 then c2 then t2 else f2 else c3 then t3 else f3
There is no ambiguity, because an else statement
always persent for each then statement.
The later syntax is hard to understand. For example:
a then b then c else d else e then f else g
- "What's this?" ;-)
One inline condition should be written without parenthesis:
r = c then t else f
Nested inline conditions should be placed
in parenthesis to ensure readability:
r = c1 then (c2 then t2 else f2) else (c3 then t3 else f3)
In my opinion the language syntax should not force the
parenthesis, but they are strongly recommended in the
With correctly placed ( ) pairs inline conditions are not
harder to read and not more "COBOL like" than a list
More information about the Python-list