Also support !a, since it is in Python 3.

This commit is contained in:
Eric V. Smith 2015-08-20 20:13:37 -04:00
parent 2b710a1d86
commit 10e7fb2a55
1 changed files with 28 additions and 24 deletions

View File

@ -41,8 +41,8 @@ values. Some examples are::
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."
This PEP proposes a new method on the str type: str.interpolate. This
method will be used to implement f-strings.
This PEP proposes a new method on the str type:
str.interpolate(). This method will be used to implement f-strings.
A similar feature was proposed in PEP 215 [#]_. PEP 215 proposed to
support a subset of Python expressions, and did not support the
@ -169,12 +169,13 @@ corresponding single brace. Doubled opening braces do not signify the
start of an expression.
Following the expression, an optional type conversion may be
specified. The allowed conversions are '!s' or '!r'. These are
specified. The allowed conversions are '!s', '!r', or '!a'. These are
treated the same as in str.format: '!s' calls str() on the expression,
and '!r' calls repr() on the expression. These 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.
'!r' calls repr() on the expression, and '!a' calls ascii() on the
expression. These 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.
Similar to str.format, optional format specifiers maybe be included
inside the f-string, separated from the expression (or the type
@ -183,7 +184,7 @@ provied, an empty string is used.
So, an f-string looks like::
f ' <text> { <expression> <optional !s or !r> <optional : format specifier> } text ... '
f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } text ... '
The resulting expression's __format__ method is called with the format
specifier. The resulting value is used when building the value of the
@ -196,14 +197,15 @@ special cased.
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.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 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 contain the
characters ':' or '}', nor the strings '!s' and '!r'.
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.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
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
contain the characters ':' or '}', nor the strings '!s', '!r', or
'!a'.
Code equivalence
----------------
@ -425,11 +427,11 @@ compatible with a bytes string.
#XXX: maybe allow this, but encode the output as ascii?
!s and !r are redundant
-----------------------
!s, !r, and !s are redundant
----------------------------
The !s and !r are not strictly required. Because arbitrary expressions
are allowed inside the f-strings, this code::
The !s, !r, and !a are not strictly required. Because arbitrary
expressions are allowed inside the f-strings, this code::
>>> a = 'some string'
>>> f'{a!r}'
@ -440,11 +442,13 @@ Is identical to::
>>> f'{repr(a)}'
"'some string'"
Similarly, !s can be replaced by calls to str().
Similarly, !s can be replaced by calls to str() and !a by calls to
ascii().
However, !s and !r are supported by this PEP in order to minimize the
differences with str.format(). !s and !r are required in str.format()
because it does not allow the execution of arbitrary expressions.
However, !s, !r, and !a are supported by this PEP in order to minimize
the differences with str.format(). !s, !r, and !a are required in
str.format() because it does not allow the execution of arbitrary
expressions.
Lambdas inside expressions
--------------------------