PEP 308: A PEP Writer's Experience - PRO
Bengt Richter
bokr at oz.net
Sun Feb 9 15:41:08 EST 2003
On Sun, 9 Feb 2003 17:30:03 GMT, Andrew Koenig <ark at research.att.com> wrote:
>Gerrit> I disagree, because maybe both X and Y are in all cases a bad
>Gerrit> way to write it, so that it's not necessary to enlarge the
>Gerrit> language so that X can be written as Y if Z is still better.
>
>Here's a different example that you may find more persuasive.
>
>Recall that many programs take file names as parameters, and some of
>those use the convention that if the file name is "-", the program
>should process the standard input instead of opening a file.
>
>Suppose we want to print the identity of the file that we are about to
>process. In other words, we want to print "Processing", followed by
>either "<stdin>" or the file name, depending on whether the
>file name is "-".
>
>Then I would like to be able to write this:
>
> print "Processing", "<stdin>" if filename == "-" else filename
>
To me, that reads like
print "Processing", "<stdin>" if filename == "-"
else filename
i.e., I am used to if-else selecting alternative actions, not alternative data,
so the first action candidate is print something, and the else action candidate
is <cognitive dissonance here>. I.e., the else reads as if introducing the
alternative to print (an action), not to "<stdin>" (data).
I'd rather have
print "Processing", (filename == "-" ? filename, "<stdin>") # (cond ? sel_F, sel_T)
With a multiway integer selection spelled iexpr ?? sel0, sel1, sel2 # etc
>You might claim that it is better to write
>
> print "Processing",
> if filename == "-":
> print "<stdin>"
> else:
> print filename
>
>or, alternatively, to write
>
> if filename == "-":
> printname = "<stdin>"
> else:
> printname = filename
> print "Processing", printname
>
>or, perhaps,
>
> def printname(filename):
> if filename == "-":
> return "<stdin>"
> return filename
> print "Processing", printname(filename)
>
>but I disagree. I think it's premature factoring. The time to factor
Good to have a name for it ;-)
>out the concept of "the printable version of a filename" is when you
>know you're going to use it more than once (and possibly more than
>twice).
>
>Of course, I don't consider this argument to be proof. There isn't
>going to be any proof in this discussion, because the whole question
>is *always* about which of two equivalent forms to prefer.
Or three, or more ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list