[Python-Dev] Issue 6081: format string using mapping rather than kwargs
Eric Smith
eric at trueblade.com
Thu Mar 25 13:57:00 CET 2010
Nick Coghlan wrote:
> Filip Gruszczyński wrote:
>> I would appreciate any advice on this topic, even if this ticket would
>> be dismissed altogether, as I would like to learn as much as possible
>> on practices on developing Python.
>
> Raymond's use case is valid, but the currently proposed method name is
> way too long to be usable and the use of *args is a definite misfeature.
>
> I suggest approaching the issue with the exact spec needed to cover the
> problem Raymond described:
>
> Add a method to strings that allows a format string to
> be applied to an existing mapping without implicitly
> converting the mapping to a dict first.
It occurs to me that Raymond's use case could be satisfied using
existing Python, by slightly changing the format string. After all,
str.format() supports mapping lookup already:
$ ./python.exe
Python 2.6.5+ (release26-maint:79421, Mar 25 2010, 08:51:39)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Default(dict):
... def __missing__(self, key):
... return key
...
>>> s = '{m[name]} was born in {m[country]}'
>>> s.format(m=Default(name='Guido'))
'Guido was born in country'
>>>
Considering that, maybe the best thing is to do nothing. I'll update the
issue with this note.
--
Eric.
More information about the Python-Dev
mailing list