[Python-checkins] peps: Clean up usage of format_spec and field_name. Add additional verbiage about

eric.smith python-checkins at python.org
Fri Aug 21 02:55:52 CEST 2015


https://hg.python.org/peps/rev/19465f3c9f4f
changeset:   5961:19465f3c9f4f
user:        Eric V. Smith <eric at trueblade.com>
date:        Thu Aug 20 20:55:54 2015 -0400
summary:
  Clean up usage of format_spec and field_name. Add additional verbiage about i18n and concatenating adjacent f-strings.

files:
  pep-0498.txt |  35 +++++++++++++++++++++++++++--------
  1 files changed, 27 insertions(+), 8 deletions(-)


diff --git a/pep-0498.txt b/pep-0498.txt
--- a/pep-0498.txt
+++ b/pep-0498.txt
@@ -82,7 +82,9 @@
 %-formatting. In particular, it uses normal function call syntax (and
 therefor supports mutliple parameters) and it is extensible through
 the __format__() method on the object being converted to a string. See
-PEP-3101 for a detailed rationale.
+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
 its verbosity. For example, the text 'value' is repeated here::
@@ -197,13 +199,13 @@
 str.interpolate()
 -----------------
 
-str.interpolate() will be a new method. It takes one argument: a
-mapping of field names to values. This method is the same as
+str.interpolate(mapping) will be a new method. It takes one argument:
+a mapping of field names to values. This method is the same as
 str.format_map() [#]_, with one difference: it does not interpret the
-field_name [#]_ in any way. The field name is only used to look up the
+field_name [#]_ in any way. The field_name is only used to look up the
 replacement value in the supplied mapping object. Like str.format()
-and str.format_map(), it does interpret and apply the optional
-conversion character and format specifier. Thus, a field name may not
+and str.format_map(), str.interpolate() does interpret and apply the
+optional conversion and format_spec. Thus, a field_name may not
 contain the characters ':' or '}', nor the strings '!s', '!r', or
 '!a'.
 
@@ -281,12 +283,24 @@
 
   'ab10cstr< hi >de'
 
-While the exact method of this runtime concatenation is unspecified,
+While the exact method of this run time concatenation is unspecified,
 the above code might evaluate to::
 
 
   ''.join(['ab', '{x}'.interpolate({'x': x}), 'c', 'str<', 'str<{y:^4}>'.interpolate({'y': y}), 'de'])
 
+You are guaranteed, however, that there will be no compile time
+combination of f-strings::
+
+  >>> x = 0
+  >>> y = 1
+  >>> f'{x}' f'{y}'
+  '01'
+
+Will result in 2 calls to str.interpolate(): once on the string '{x}',
+and again on the string '{y}'. This guarantee is needed to facilitate
+proposed future internationalization.
+
 Error handling
 --------------
 
@@ -484,7 +498,12 @@
 resulting string would be 'Eric es mi nombre'.
 
 Any such internationalization work will be specified in an additional
-PEP.
+PEP. In all likelyhood, such a PEP will need to propose one or more
+additional optional parameters to str.interpolate() in order to handle
+the string.Template case of "safe substitution", where the substituted
+field_names are not found in the mapping argument. The choices might
+be: use the field_name, use a default (possibly empty) string, or
+raise an exception.
 
 References
 ==========

-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list