[Python-ideas] Briefer string format

Stefan Behnel stefan_ml at behnel.de
Sun Aug 9 10:00:33 CEST 2015


Mike Miller schrieb am 09.08.2015 um 02:48:
> On 08/08/2015 09:49 AM, Stefan Behnel wrote:
>> Mike Miller schrieb am 20.07.2015 um 01:35:
>>>      csstext += '{nl}{key}{space}{{{nl}'.format(**locals())
>>>
>>> This looks a bit better if you ignore the right half, but it is longer
>>> and not
>>>
>>>     csstext += '{nl}{key}{space}{{{nl}'.format(nl=nl, key=key, ...  # uggh
>>>     csstext += '{}{}{}{{{}'.format(nl, key, space, nl)
>>
>> Is this an actual use case that people *commonly* run into? I understand
>> that the implicit name lookups here are safe and all that, but I cannot
>> recall ever actually using locals() for string formatting.
> 
> There are several ways to accomplish that line.  If you look below it there
> two alternatives, that are suboptimal as well.
> 
>> The above looks magical to me. It's completely unclear that string
>> ...
>> I'd prefer not seeing a "cool feature" added just "because it's cool". If
>> it additionally is magic, it's usually not a good idea.
> 
> Direct string interpolation is a widely desired feature, something the
> neckbeards of old, hipsters, and now suits have all agreed on.

But how common is it, really? Almost all of the string formatting that I've
used lately is either for logging (no help from this proposal here) or
requires some kind of translation/i18n *before* the formatting, which is
not helped by this proposal either. Meaning, in almost all cases, the
formatting will use some more or less simple variant of this pattern:

    result = process("string with {a} and {b}").format(a=1, b=2)

which commonly collapses into

    result = translate("string with {a} and {b}", a=1, b=2)

by wrapping the concrete use cases in appropriate helper functions.

I've seen Nick Coghlan's proposal for an implementation backed by a global
function, which would at least catch some of these use cases. But it
otherwise seems to me that this is a huge sledge hammer solution for a
niche problem.

Stefan




More information about the Python-ideas mailing list