Incorporate feedback from Guido. Still several more items to tackle.

This commit is contained in:
Eric V. Smith 2015-08-20 21:14:43 -04:00
parent cb5ee58dbd
commit 14c0910963
1 changed files with 27 additions and 4 deletions

View File

@ -251,6 +251,27 @@ Is equivalent to::
>>> 'result=' + str(foo()) >>> 'result=' + str(foo())
'result=20' '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 Format specifiers
----------------- -----------------
@ -277,17 +298,16 @@ concatenated at run time. For example, the expression::
>>> x = 10 >>> x = 10
>>> y = 'hi' >>> 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:: yields the value::
'ab10cstr< hi >de' 'ab10{c}str< hi >de'
While the exact method of this run time concatenation is unspecified, While the exact method of this run time concatenation is unspecified,
the above code might evaluate to:: 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 You are guaranteed, however, that there will be no compile time
combination of f-strings:: combination of f-strings::
@ -535,6 +555,9 @@ References
.. [#] Format string syntax .. [#] Format string syntax
(https://docs.python.org/3/library/string.html#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 .. [#] Start of python-ideas discussion
(https://mail.python.org/pipermail/python-ideas/2015-July/034657.html) (https://mail.python.org/pipermail/python-ideas/2015-July/034657.html)