New Python 3.0 string formatting - really necessary?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Dec 20 21:26:34 EST 2008


On Sat, 20 Dec 2008 17:55:35 -0800, Aaron Brady wrote:

> On Dec 20, 7:38 pm, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
>> Instead of just whinging, how about making a suggestion to fix it? Go
>> on, sit down for an hour or ten and try to work out how a BINARY
>> OPERATOR like % (that means it can only take TWO arguments) can deal
>> with an arbitrary number of arguments, *without* having any special
>> cases.
>>
>> Go on. Take your time. I'll be waiting.
> 
> Hi, not to take sides, but, there is a possibility.
> 
> 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.

"%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'

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.


(BTW, if you want that behaviour, you should look at the string module.)


-- 
Steven



More information about the Python-list mailing list