Improve comparison to string.Template and %-formatting.
This commit is contained in:
parent
7261e0d009
commit
1d6b74f376
23
pep-0498.txt
23
pep-0498.txt
|
@ -86,7 +86,7 @@ PEP-3101 for a detailed rationale. This PEP reuses much of the
|
|||
str.format() syntax and machinery, in order to provide continuity with
|
||||
an existing Python string formatting mechanism.
|
||||
|
||||
However, str.format() is not without its issues. Chief among them are
|
||||
However, str.format() is not without its issues. Chief among them is
|
||||
its verbosity. For example, the text 'value' is repeated here::
|
||||
|
||||
>>> value = 4 * 20
|
||||
|
@ -108,9 +108,24 @@ With an f-string, this becomes::
|
|||
f-strings provide a concise, readable way to include expressions
|
||||
inside strings.
|
||||
|
||||
string.Template has similar shortcomings to str.format(), but also
|
||||
supports fewer formatting options. In particular, it does not support
|
||||
__format__.
|
||||
In this sense, string.Template and %-formatting have similar
|
||||
shortcomings to str.format(), but also support fewer formatting
|
||||
options. In particular, they do not support __format__, so that there
|
||||
is no way to control how a specific object is converted to a string,
|
||||
nor can it be extended to additional types that want to control how
|
||||
they are converted to strings (such as Decimal and datetime). This
|
||||
example is not possible with string.Template::
|
||||
|
||||
>>> value = 1234
|
||||
>>> f'input={value:#0.6x}'
|
||||
'input=0x04d2'
|
||||
|
||||
And neither %-formatting nor string.Template can control formatting
|
||||
such as::
|
||||
|
||||
>>> date = datetime.date(1991, 10, 12)
|
||||
>>> f'{date} was on a {date:%A}'
|
||||
'1991-10-12 was on a Saturday'
|
||||
|
||||
No use of globals() or locals()
|
||||
-------------------------------
|
||||
|
|
Loading…
Reference in New Issue