[Patches] [ python-Patches-639307 ] new string method -- format

noreply@sourceforge.net noreply@sourceforge.net
Sun, 17 Nov 2002 12:37:27 -0800


Patches item #639307, was opened at 2002-11-16 13:20
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=639307&group_id=5470

Category: Core (C code)
Group: Python 2.3
>Status: Closed
>Resolution: Rejected
Priority: 3
Submitted By: Jp Calderone (kuran)
Assigned to: Nobody/Anonymous (nobody)
Summary: new string method -- format

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Martin v. L÷wis (loewis)
Date: 2002-11-17 21:37

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

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2002-11-17 19:09

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


----------------------------------------------------------------------

Comment By: Martin v. L÷wis (loewis)
Date: 2002-11-16 17:42

Message:
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__.

----------------------------------------------------------------------

Comment By: Jp Calderone (kuran)
Date: 2002-11-16 13:28

Message:
Logged In: YES 
user_id=366566

Promised example.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=639307&group_id=5470