[Python-3000] String formating operations in python 3k

Ian Bicking ianb at colorstudy.com
Mon Apr 3 20:51:38 CEST 2006


Guido van Rossum wrote:
>>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).

Would "%(foo)s".format({'foo': 123}) work?  Or would you need **{...}?

FWIW, I suspect I'd be much more likely to use named %'s with .format() 
than with %; which is probably good, since named markers are more 
flexible.  E.g., I never do: "%(path)s: path %(path)r does not exist" % 
{'path': path}, but I often do "%s: path %r does not exist" % (path, 
path).  But the first form is really better in several ways.

If .substitute() (or .sub()?) was available to do $-based substitution 
alongside .format() for %-based substitution, that would both have a 
nice symmetry and make it more comfortable to move between the two.

>>>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.

Syntactic support does allow for a level of flexibility (evaluation) 
plus security (special casing literals) that no method can provide.


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list