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

noreply@sourceforge.net noreply@sourceforge.net
Sat, 16 Nov 2002 08:42:23 -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: Open
Resolution: None
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-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