[Python-checkins] peps: Make it clear that __format__ isn't called on the value of each expression, but

eric.smith python-checkins at python.org
Sat Sep 19 17:24:13 CEST 2015


https://hg.python.org/peps/rev/0060ddc240a7
changeset:   6070:0060ddc240a7
user:        Eric V. Smith <eric at trueblade.com>
date:        Sat Sep 19 11:24:19 2015 -0400
summary:
  Make it clear that __format__ isn't called on the value of each expression, but rather on the type of the value. This is how the builtin format() function works, too.

files:
  pep-0498.txt |  19 ++++++++++++-------
  1 files changed, 12 insertions(+), 7 deletions(-)


diff --git a/pep-0498.txt b/pep-0498.txt
--- a/pep-0498.txt
+++ b/pep-0498.txt
@@ -8,7 +8,7 @@
 Content-Type: text/x-rst
 Created: 01-Aug-2015
 Python-Version: 3.6
-Post-History: 07-Aug-2015, 30-Aug-2015, 04-Sep-2015
+Post-History: 07-Aug-2015, 30-Aug-2015, 04-Sep-2015, 19-Sep-2015
 Resolution: https://mail.python.org/pipermail/python-dev/2015-September/141526.html
 
 Abstract
@@ -201,6 +201,11 @@
 replaced by the corresponding single brace. Doubled opening braces do
 not signify the start of an expression.
 
+Note that ``__format__()`` is not called directly on each value. The
+actual code uses the equivalent of ``type(value).__format__(value,
+format_spec)``, or ``format(value, format_spec)``. See the
+documentation of the builtin ``format()`` function for more details.
+
 Comments, using the ``'#'`` character, are not allowed inside an
 expression.
 
@@ -209,7 +214,7 @@
 ``'!a'``. These are treated the same as in ``str.format()``: ``'!s'``
 calls ``str()`` on the expression, ``'!r'`` calls ``repr()`` on the
 expression, and ``'!a'`` calls ``ascii()`` on the expression. These
-conversions are applied before the call to ``__format__``. The only
+conversions are applied before the call to ``format()``. The only
 reason to use ``'!s'`` is if you want to specify a format specifier
 that applies to ``str``, not to the type of the expression.
 
@@ -222,9 +227,9 @@
 
   f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '
 
-The resulting expression's ``__format__`` method is called with the
-format specifier as an argument. The resulting value is used when
-building the value of the f-string.
+The expression is then formatted using the ``__format__`` protocol,
+using the format specifier as an argument. The resulting value is
+used when building the value of the f-string.
 
 Expressions cannot contain ``':'`` or ``'!'`` outside of strings or
 parentheses, brackets, or braces. The exception is that the ``'!='``
@@ -293,7 +298,7 @@
 
 Might be be evaluated as::
 
-  'abc' + expr1.__format__(spec1) + repr(expr2).__format__(spec2) + 'def' + str(expr3).__format__('') + 'ghi'
+  'abc' + format(expr1, spec1) + format(repr(expr2)) + 'def' + format(str(expr3)) + 'ghi'
 
 Expression evaluation
 ---------------------
@@ -371,7 +376,7 @@
 While the exact method of this run time concatenation is unspecified,
 the above code might evaluate to::
 
-  'ab' + x.__format__('') + '{c}' + 'str<' + y.__format__('^4') + '>de'
+  'ab' + format(x) + '{c}' + 'str<' + format(y, '^4') + '>de'
 
 Each f-string is entirely evaluated before being concatenated to
 adjacent f-strings. That means that this::

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


More information about the Python-checkins mailing list