PEP 308: some candidate uses cases from live code

Martin Maney maney at
Mon Feb 10 01:08:00 CET 2003

Bengt Richter <bokr at> wrote:
> On Sun, 9 Feb 2003 08:10:17 +0000 (UTC), Martin Maney <maney at> wrote:
>>    About all that could be changed here is collapsing the if/else, as i is
>>    live past the end of the code shown:
>>        else:
>>            start = i
>>            j = s.find(',', i)
>             field = s[start:(j<0?j,n)]  # cond ? false_sel, true_sel

Except as I said, hoping to forestall this very misunderstanding, i is
live at the end of this code, so you can't skip its assignment.  So
what you meant to say would be

>>            i = j if j >= 0 else n
>>            field = s[start:i]

            i = (j < 0 ? j, n)
            field = s[start:i]

BTW, the false and true are given backwards in your proposal. <wink>
-1 on that ordering, +0 on the form with corrected ordering.  The
parentheses are okay, the question mark is "obvious", but the comma is
too nearly invisble to please me.

>>Candidate 2 - generating bounding dates for a month
>>        first = '%04d-%02d-01' % (year, month)
>         last = '%04d-%02d-01' %  (month<12? (year+1, 1), (year, month+1))

Yeah, now let's try writing it with readable spaces:

        last = "%04d-%02d-01' % (month < 12 ? (year + 1, 1), (year, month + 1))

I still don't care for it.  It makes it much harder to see what is
actually different between the two cases.  Of course that's equally
true no matter what form the ternary takes.

>>        sy = d0.year - (1 if d0.month < 7 else 0)
>         sy = d0.year - (d0.month < 7) # this is already 1 or 0

Bad trick.  Page has been ripped out of playbook long since.  :-(

>         sy = d0.year - (d0.month < 7 ? 0, 1) # this is already 1 or 0
>         sy = d0.month<7 ? d0.year, d0.year-1 # alternative form

Oh, were the parentheses situational?  -0.5 on the form without
parentheses and with comma, then.  IMO.  And the ordering is Just Wrong.

I think the rest are pretty much just more of the same.  Of course I
cotton to the form in general: I've written 'way too much C and C++
over the years *.  The comma really doesn't seem adequate to the role
you give it; maybe that's for the same reasons.

*) didn't really use the ternary op all that often then, either.  It's
one of those things you can live without, but it's sure handy to have
it around.  I've been trying to think of something in uncontroversial
Python that feels about the same, and the best match I've come up with
is, well, it's a bit of an overstatement.  But you know, now that we
have iterators (well, for some values of "us"), there's really no need
for the while statement anymore.  It's redundant, just syntactic sugar
to replace

    for dummy in infinite_iterator:
        if inverted_condition:
        ... body of loop here...

winking'ly yours

More information about the Python-list mailing list