New Python 3.0 string formatting - really necessary?

Aaron Brady castironpi at gmail.com
Sun Dec 21 12:23:43 CET 2008


On Dec 20, 8:26 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sat, 20 Dec 2008 17:55:35 -0800, Aaron Brady wrote:
snip
> > This behavior is currently legal:
>
> >>>> "%i %%i" % 0 % 1
> > '0 1'
>
> > So, just extend it.  (Unproduced.)
>
> >>>> "%i %i" % 0 % 1
> > '0 1'
>
> Errors should never pass silently, unless explicitly silenced. You have
> implicitly silenced the TypeError you get from not having enough
> arguments for the first format operation. That means that you will
> introduce ambiguity and bugs.

No, it's not part of the (new) '%' operation.  '%' handles one flag at
a time.  It's not an error if the author intends it.

> "%i %i %i %i" % 5 % 3 %7
>
> Here I have four slots and only three numbers. Which output did I expect?
>
> '%i 5 3 7'
> '5 %i 3 7'
> '5 3 %i 7'
> '5 3 7 %i'

Anything, so long as it's (contraction) consistent and general.

> Or more likely, the three numbers is a mistake, there is supposed to be a
> fourth number there somewhere, only now instead of the error being caught
> immediately, it won't be discovered until much later.

Leave that to unit testing and your QA team.

To make the change, the burden of proof (which is large) would fall to
me.  However, in the abstract case, it's not clear that either one is
favorable, more obvious, or a simpler extrapolation.

Bug-proneness is an argument against a construction, just not a
conclusive one.  How heavy is it in this case?



More information about the Python-list mailing list