[Python-ideas] %-formatting with Decimals
Chris Angelico
rosuav at gmail.com
Tue Mar 11 23:47:33 CET 2014
On Wed, Mar 12, 2014 at 9:30 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> mod-formatting is now a power tool for optimisation, consistency with C
> implementations, templating format strings without excessive escaping and
> (the main reason it still exists), backwards compatibility (both for
> existing code and existing programmers).
And consistency with other C-derived languages, too. The only
weirdness of Python's that isn't found in others is the "hey look
isn't it cool" use of the % operator, which results in edge cases
because the operator always takes exactly one argument on the RHS -
which can be solved by turning it into a function:
>>> def sprintf(fmt, *args):
return fmt%args
>>> sprintf("foo %s bar",(1,2,3))
'foo (1, 2, 3) bar'
>>> sprintf("foo %d bar %d quux",1,2)
'foo 1 bar 2 quux'
ChrisA
More information about the Python-ideas
mailing list