[Python-checkins] peps: Improve comparison to string.Template and %-formatting.
eric.smith
python-checkins at python.org
Fri Aug 21 11:13:36 CEST 2015
https://hg.python.org/peps/rev/249092f2e117
changeset: 5966:249092f2e117
user: Eric V. Smith <eric at trueblade.com>
date: Fri Aug 21 05:13:39 2015 -0400
summary:
Improve comparison to string.Template and %-formatting.
files:
pep-0498.txt | 23 +++++++++++++++++++----
1 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/pep-0498.txt b/pep-0498.txt
--- a/pep-0498.txt
+++ b/pep-0498.txt
@@ -86,7 +86,7 @@
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 @@
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()
-------------------------------
--
Repository URL: https://hg.python.org/peps
More information about the Python-checkins
mailing list