PEP 308: some candidate uses cases from live code

Martin Maney maney at pobox.com
Mon Feb 10 23:35:37 EST 2003


Kyler Laird <Kyler at news.lairds.org> wrote:
> Anyone want to claim that this is preferable for some?
>        if d0.month < 7:
>                sy = d0.year - 1
>        else:
>                sy = d0.year

Well, since that was in fact how I wrote it...  <wink>

> I cringe at the duplication of "d0.year" but I've been
> teaching Scheme recently and noticed that students seem
> very comfortable saying "If this is the case, then the
> answer is this, otherwise..." instead of "Always do this
> and then if whatever, modify the result."

It reads the way we think, at least in English.  It takes years of
experience to learn to think of it in mercilessly refactored until
there's no redundancy left form, and then more years to see the
foolishness of pushing it that far in every case.

BTW, I wanted to say somewhere in this thread that I'm not at all sure
whether I'd choose to write *any* of these candidates using the ternary
form.  The "sep" example is probably the most likely one, assuming it
was actually written with only a single use of "sep".  I suspect it
used to be used at least twice in an earlier version.

I know that this would be useful for lambdas, but I seem to have
rewritten all such uses (or maybe overlooked them - I wasn't
specifically looking for that sort of thing, alas).  That's one flaw in
this survey: it's on a body of code that has mostly been rewritten
(well, heavily refactored, anyway) at least once... by someone who
loathes the existing kluges enough that I very likely tried them and
got rid of them... probably back in August/September, when I was
excited about some of that stuff from having just read the Cookbook.  I
know a fair number of lambdas vanished as list comprehensions came into
vogue here!

I *know* I have had to write a function for things that would be
trivial inline lambdas with the PEP extension, but they're all gone
now.  So the candidates I found are probably all near-misses int he
first place.

It's not easy to find good existing examples for a language feature
that doesn't exist!




More information about the Python-list mailing list