New Python 3.0 string formatting - really necessary?
MRAB
google at mrabarnett.plus.com
Sat Dec 20 21:49:57 EST 2008
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'
>>>> "%r %i" % (2, 3, 4) % 1
> '(2, 3, 4) 1'
>>>> "%r %i" % (2, 3, 4)
> '(2, 3, 4) %i'
>
> Which is quite clever and way ahead of its (posessive) time.
>
A couple of problems:
1. How do you handle a literal '%'? If you just double up then you'll
need to fix the string after all your substitutions.
2. What if a substitution introduces a '%'?
I suppose a possible solution would be to introduce a special format
string, including a literal, eg:
f"%r %i" % (2, 3, 4) % 1
and then convert the result to a true string:
print(str(f"%r %i" % (2, 3, 4) % 1))
(although print() would call __str__ anyway).
The format string would track where the last substitution occurred.
Hmm... I think I'll just learn the new method. :-)
More information about the Python-list
mailing list