Incorporate feedback from Guido. Still several more items to tackle.
This commit is contained in:
parent
cb5ee58dbd
commit
14c0910963
31
pep-0498.txt
31
pep-0498.txt
|
@ -251,6 +251,27 @@ Is equivalent to::
|
|||
>>> 'result=' + str(foo())
|
||||
'result=20'
|
||||
|
||||
After stripping leading and trailing whitespace (see below), the
|
||||
expression is parsed with the equivalent of ast.parse(expression,
|
||||
'<fstring>', 'eval') [#]_. Note that this restricts the expression: it
|
||||
cannot contain any newlines, for example::
|
||||
|
||||
>>> x = 0
|
||||
>>> f'''{x
|
||||
... +1}'''
|
||||
File "<fstring>", line 2
|
||||
+1
|
||||
^
|
||||
SyntaxError: invalid syntax
|
||||
|
||||
But note that this works, since the newline is removed from the
|
||||
string, and the spaces in from of the '1' are allowed in an
|
||||
expression::
|
||||
|
||||
>>> f'{x+\
|
||||
... 1}'
|
||||
'2'
|
||||
|
||||
Format specifiers
|
||||
-----------------
|
||||
|
||||
|
@ -277,17 +298,16 @@ concatenated at run time. For example, the expression::
|
|||
|
||||
>>> x = 10
|
||||
>>> y = 'hi'
|
||||
>>> 'a' 'b' f'{x}' 'c' f'str<{y:^4}>' 'd' 'e'
|
||||
>>> 'a' 'b' f'{x}' '{c}' f'str<{y:^4}>' 'd' 'e'
|
||||
|
||||
yields the value::
|
||||
|
||||
'ab10cstr< hi >de'
|
||||
'ab10{c}str< hi >de'
|
||||
|
||||
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'])
|
||||
''.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::
|
||||
|
@ -535,6 +555,9 @@ References
|
|||
.. [#] Format string syntax
|
||||
(https://docs.python.org/3/library/string.html#format-string-syntax)
|
||||
|
||||
.. [#] ast.parse() documentation
|
||||
(https://docs.python.org/3/library/ast.html#ast.parse)
|
||||
|
||||
.. [#] Start of python-ideas discussion
|
||||
(https://mail.python.org/pipermail/python-ideas/2015-July/034657.html)
|
||||
|
||||
|
|
Loading…
Reference in New Issue