[New-bugs-announce] [issue39842] partial_format()
Marco Sulla
report at bugs.python.org
Tue Mar 3 18:57:59 EST 2020
New submission from Marco Sulla <launchpad.net at marco.sulla.e4ward.com>:
In `string` module, there's a very little known class `Template`. It implements a very simple template, but it has an interesting method: `safe_substitute()`.
`safe_substitute()` permits you to not fill the entire Template at one time. On the contrary, it substitute the placeholders that are passed, and leave the others untouched.
I think it could be useful to have a similar method for the format minilanguage. I propose a partial_format() method.
=== WHY I think this is useful? ===
This way, you can create subtemplates from a main template. You could want to use the template for creating a bunch of strings, all of them with the same value for some placeholders, but different values for other ones. This way you have *not* to reuse the same main template and substitute every time the placeholders that does not change.
`partial_format()` should act as `safe_substitute()`: if some placeholder misses a value, no error will be raised. On the contrary, the placeholder is leaved untouched.
Some example:
>>> "{} {}".partial_format(1)
'1 {}'
>>> "{x} {a}".partial_format(a="elephants")
'{x} elephants'
>>> "{:-f} and {:-f} nights".partial_format(1000)
'1000 and {:-f} nights'
----------
components: Interpreter Core
messages: 363317
nosy: Marco Sulla
priority: normal
severity: normal
status: open
title: partial_format()
type: enhancement
versions: Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39842>
_______________________________________
More information about the New-bugs-announce
mailing list