PEP 308: A PEP Writer's Experience - PRO

John La Rooy nospampls.jlr at doctor.com
Mon Feb 10 17:54:22 EST 2003


On Mon, 10 Feb 2003 17:58:53 GMT
Andrew Koenig <ark at research.att.com> wrote:

> Fredrik> even if the size of X is close to zero, for anyone who has
> Fredrik> bothered to *learn* the language?
> 
> Fredrik> or are you saying that learning is irrelevant?
> 
> Well, no -- I'm saying partly that learning does not always overcome
> the tendency to make mistakes that are waiting to be made.
> 
> For example, look at this code from Python 2.2.2 Lib/unittest.py:
> 
>         self.stream.writeln("Ran %d test%s in %.3fs" %
>                             (run, run == 1 and "" or "s", timeTaken))
> 
> See the bug?
> 
> -- 
> Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark

Point to Andrew!

I would suggest using one of these forms

    self.stream.writeln("Ran %d test%s in %.3fs" %
                        (run, ("s","")[run==1], timeTaken))

or
    self.stream.writeln("Ran %d test%s in %.3fs" %
                        (run, "s"[run==1:], timeTaken))

There is negligible difference in performance. I think the indexed one is
a little clearer.

Do we really want to start seeing this?

    self.stream.writeln("Ran %d test%s in %.3fs" %
                        (run, "" if run == 1 else "s", timeTaken))


John:






More information about the Python-list mailing list