PEP 308: A PEP Writer's Experience - PRO

Andrew Koenig ark at research.att.com
Sun Feb 9 12:30:03 EST 2003


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

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
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.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list