[Tim Peters]
> [Martin Maney]
> > ...
> > Becomes, after a little rearrangement:
> >
> > sy = d0.year - (1 if d0.month < 7 else 0)
> >
> > I think this actually expresses the original logic a little more
> > clearly: the school year is the day's year unless the day's month is
> > January through June; then one must be subtracted to get the
calendar
> > year during which the school year began.
>> Me too, but note that it could be written (even in Python 1.0):
>> sy = d0.year - (d0.month < 7)
>> That bools are confusable with 0 and 1 in Python is A Feature.
With the ?: syntax the rearrangement above becomes:
sy = d0.year - (d0.month < 7 ? 1 : 0)
And the refactoring you did becomes much more obvious.
For me, that a killer problem of PEP308's proposed syntax; with the
condition in the middle, reading *and* refactoring the code becomes
harder! That is why I cannot vote simply for PEP308.
The only syntaxes I'd like to see are:
condition "?" then-expr ":" else-expr
condition "?" then-expr "else" else-expr
I prefer ?: because:
- I liked it better after playing with ?: interactively[1]
- ":" balances "?" better than "else"
- consistent with the C-like language crowd, like python's operators
in
general.
One argument against ?: (yet another job for the colon) doesn't
convince
me. The three other places I see where colons are used are not likely
to
see ?: expressions in my opinion, and even then simply adding
parentheses
can restore readability:
- slicing/indexing: a[(t?x:y)]
- if statements: if (t?x:y):
- dictionary literals: {(t?x:y): value}
In real code ?: replaces three idioms. IMO ?: is more obvious than
using
and/or, more readable than (false, true)[condition], with better
semantics
than either idiom. The ?: syntax offers enough improvement to supplant
every
use of the aforementioned syntaxes in my python code.
That leaves this idiom:
if test:
lhs = then-expr
else:
lhs = else-expr
In some cases I think that kind of code is improved by using a
conditional
expression, especially when a complex lhs or common subexpression in
then-expr and else-expr can be factored out.
-Tony Lownds
[1] ESR has a patch to implement ?: that is easy to modify for current
CVS python
http://mail.python.org/pipermail/python-dev/2000-January/001883.html