This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: new string method -- format
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: exarkun, loewis, terry.reedy
Priority: low Keywords: patch

Created on 2002-11-16 12:20 by exarkun, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
format.function exarkun, 2002-11-16 12:20 patch to add str.format and unicode.format
example.py exarkun, 2002-11-16 12:28
Messages (5)
msg41683 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2002-11-16 12:20
Attached patch adds a method, 'format', to str and
unicode types.  The method performs the same operation
as the string interpolation operator.

The patch also includes modifications to test_format.py
as well as libstdtypes.tex (tex code untested - I can't
figure out latex; hopefully it is correct though, much
is copy/pasted from elsewhere).

Aside from having wanted this method forever, one of my
use cases is building a list of objects to be displayed
in a somewhat generic fashion.  Currently an explicit
function is required for the simple operation of string
interpolation, either by def'ing one or using a lambda,
while other, more complex operations.  Example attached.
msg41684 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2002-11-16 12:28
Logged In: YES 
user_id=366566

Promised example.
msg41685 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-11-16 16:42
Logged In: YES 
user_id=21627

There should be one-- and preferably only one --obvious way
to do it.

The advantage of adding this method is not clear; you can
easily achieve the same effect with

class BoundMod:
    def __init__(self, obj):
        self.obj = obj
    def __call__(self, otherarg):
        return self.obj % otherarg

...
    def getDisplayList(self):
        return [
            BoundMod('<a href="%s">Link</a>'),
            self.complexOutput
        ]

Alternatively, 
            lamda x:'<a href="%s">Link</a>' % x
has the same effect.

If you still want that feature, I suggest that you write a
PEP. There are a number of alternatives to consider, for
example calling the method __mod__.
msg41686 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2002-11-17 18:09
Logged In: YES 
user_id=593130

From reading the one sentence description, it was not 
clear to me whether .format() was to be method of a 
format string or a string to be formatted.  Adding 
formstring.format(*args) as a synonym for formstring % 
args doesn't make much obvious sense.  Besides the 
redundancy, I dislike this because this special-purpose 
method would only be valid for the  small subclass of 
strings that are validly used as format strings.  
     On reading the patch, the above seems to be the 
proposal.  From the example, the point seems to be to 
make it easier to curry the % operator (ie, bind it to its first 
argument).  Better to me is to leave the language alone 
(there are hundreds of such shortcuts that *could* be 
added) and either write a factory function returning bound 
function (via a nested definition) or a string subclass.
msg41687 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-11-17 20:37
Logged In: YES 
user_id=21627

Terry, thanks for your comments. It becomes clear that such
a change would meet considerable resistance in the community.

So I reject this patch now; kuran, if you want to see that
feature in Python, you will have to write a PEP.
History
Date User Action Args
2022-04-10 16:05:54adminsetgithub: 37487
2002-11-16 12:20:47exarkuncreate