Also support !a, since it is in Python 3.
This commit is contained in:
parent
2b710a1d86
commit
10e7fb2a55
52
pep-0498.txt
52
pep-0498.txt
|
@ -41,8 +41,8 @@ values. Some examples are::
|
||||||
>>> f'He said his name is {name!r}.'
|
>>> f'He said his name is {name!r}.'
|
||||||
"He said his name is 'Fred'."
|
"He said his name is 'Fred'."
|
||||||
|
|
||||||
This PEP proposes a new method on the str type: str.interpolate. This
|
This PEP proposes a new method on the str type:
|
||||||
method will be used to implement f-strings.
|
str.interpolate(). This method will be used to implement f-strings.
|
||||||
|
|
||||||
A similar feature was proposed in PEP 215 [#]_. PEP 215 proposed to
|
A similar feature was proposed in PEP 215 [#]_. PEP 215 proposed to
|
||||||
support a subset of Python expressions, and did not support the
|
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.
|
start of an expression.
|
||||||
|
|
||||||
Following the expression, an optional type conversion may be
|
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,
|
treated the same as in str.format: '!s' calls str() on the expression,
|
||||||
and '!r' calls repr() on the expression. These conversions are applied
|
'!r' calls repr() on the expression, and '!a' calls ascii() on the
|
||||||
before the call to __format__. The only reason to use '!s' is if you
|
expression. These conversions are applied before the call to
|
||||||
want to specify a format specifier that applies to str, not to the
|
__format__. The only reason to use '!s' is if you want to specify a
|
||||||
type of the expression.
|
format specifier that applies to str, not to the type of the
|
||||||
|
expression.
|
||||||
|
|
||||||
Similar to str.format, optional format specifiers maybe be included
|
Similar to str.format, optional format specifiers maybe be included
|
||||||
inside the f-string, separated from the expression (or the type
|
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::
|
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
|
The resulting expression's __format__ method is called with the format
|
||||||
specifier. The resulting value is used when building the value of the
|
specifier. The resulting value is used when building the value of the
|
||||||
|
@ -196,14 +197,15 @@ special cased.
|
||||||
str.interpolate()
|
str.interpolate()
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
str.interpolate will be a new method. It takes one argument: a mapping
|
str.interpolate() will be a new method. It takes one argument: a
|
||||||
of field names to values. This method is the same as str.format_map()
|
mapping of field names to values. This method is the same as
|
||||||
[#]_, with one difference: it does not interpret the field_name [#]_
|
str.format_map() [#]_, with one difference: it does not interpret the
|
||||||
in any way. The field name is only used to look up the replacement
|
field_name [#]_ in any way. The field name is only used to look up the
|
||||||
value in the supplied mapping object. Like str.format() and
|
replacement value in the supplied mapping object. Like str.format()
|
||||||
str.format_map(), it does interpret and apply the optional conversion
|
and str.format_map(), it does interpret and apply the optional
|
||||||
character and format specifier. Thus, a field name may not contain the
|
conversion character and format specifier. Thus, a field name may not
|
||||||
characters ':' or '}', nor the strings '!s' and '!r'.
|
contain the characters ':' or '}', nor the strings '!s', '!r', or
|
||||||
|
'!a'.
|
||||||
|
|
||||||
Code equivalence
|
Code equivalence
|
||||||
----------------
|
----------------
|
||||||
|
@ -425,11 +427,11 @@ compatible with a bytes string.
|
||||||
|
|
||||||
#XXX: maybe allow this, but encode the output as ascii?
|
#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
|
The !s, !r, and !a are not strictly required. Because arbitrary
|
||||||
are allowed inside the f-strings, this code::
|
expressions are allowed inside the f-strings, this code::
|
||||||
|
|
||||||
>>> a = 'some string'
|
>>> a = 'some string'
|
||||||
>>> f'{a!r}'
|
>>> f'{a!r}'
|
||||||
|
@ -440,11 +442,13 @@ Is identical to::
|
||||||
>>> f'{repr(a)}'
|
>>> f'{repr(a)}'
|
||||||
"'some string'"
|
"'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
|
However, !s, !r, and !a are supported by this PEP in order to minimize
|
||||||
differences with str.format(). !s and !r are required in str.format()
|
the differences with str.format(). !s, !r, and !a are required in
|
||||||
because it does not allow the execution of arbitrary expressions.
|
str.format() because it does not allow the execution of arbitrary
|
||||||
|
expressions.
|
||||||
|
|
||||||
Lambdas inside expressions
|
Lambdas inside expressions
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
Loading…
Reference in New Issue