proposal to allow to set the delimiter in str.format to something other than curly bracket

Alia Khouri alia_khouri at yahoo.com
Sun Apr 3 12:07:15 CEST 2011


Hi folks,

I've been using ironpython2.7 in a project, and I was generating some
csharp code when i discovered that I couldn't use use str.format
because the interference with the brackets-aplenty situation in
csharp.

In [1]: code = "class {0}Model { public bool IsModel(){ return
true; } }"

In [2]: code.format('My')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call
last)

KeyError: ' public bool IsModel(){ return true; } '

Please note I had no trouble with the trusty old % interpolation
method.

Now given that using str.format in this use case (code generation) is
equally difficult for other bracketed languages, and that str.format
is supposed to eventually become 'best-practice' for python string
formatting, can I get some feedback on a proposal that for the odd
case when brackets don't cut it, there is a way to redefine the
delimiter for str.format so that one could do something like this:

    str.format_delimiter = ("[[", "]]")

and we could write something like this:

In [1]: code = "class [[0]]Model { public bool IsModel(){ return
true; } }"
In [2]: code.format('My')
Out[3]: 'class MyModel { public bool IsModel(){ return true; } }'


Please give your +N or -N and reasons below.

AK



More information about the Python-list mailing list