[Python-3000] String formating operations in python 3k
Guido van Rossum
guido at python.org
Mon Apr 3 20:14:17 CEST 2006
> Crutcher Dunnavant wrote:
> >>> 1. Shouldn't there be a format method, like S.format(), or S.fmt()?
> > Why? Because:
> > 1 It is trivially cheap, format() would be the same function as __rmod__
No it shouldn't be. format() should be a varargs function; __rmod__
takes a single argument which may be a tuple. Also, format() could
take keyword args in case the string contains named format, so I can
write e.g. "%(foo)s".format(foo=123).
> > 2 It adds consistency with lower(), strip(), and other methods which
> > produce new strings.
I'm not sure that's much of an argument.
> > 3 I am not arguing _against_ syntactic support, I am arguing _for_ a method;
> > we can keep the syntactic support.
But remember TOOWTDI from the Zen of Python.
On 4/2/06, Walter Dörwald <walter at livinglogic.de> wrote:
> and it avoids one problem you might run into with %: If you have only
> one argument, writing ``s % (x,)`` as ``s % x`` will break when the
> argument x happens to be a tuple. You won't have this problem with
> s.format(x).
In fact, now that I think of it, if s.format() were available, I'd use
it in preference over s%x, just like I already use repr(x) in favor of
`x`. And just like `x` is slated for removal in Python 3000, we might
consider removing using % for formatting. The main reason probably
being the problem Walter points out (which is very real).
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list