Add notion of adding implicit parens before parsing the expression. This allows for newlines in the expressions.

This commit is contained in:
Eric V. Smith 2015-09-04 17:09:56 -04:00
parent c377e4765f
commit 2f170cfcaf
1 changed files with 11 additions and 16 deletions

View File

@ -269,26 +269,21 @@ 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::
Expressions are parsed with the equivalent of ``ast.parse('(' +
expression + ')', '<fstring>', 'eval')`` [#]_.
Note that since the expression is enclosed by implicit parentheses
before evaluation, expressions can contain newlines. For example::
>>> x = 0
>>> f'''{x
... +1}'''
File "<fstring>", line 2
+1
^
SyntaxError: invalid syntax
'1'
But note that this works, since the newline is removed from the
string, and the spaces in front of the ``'1'`` are allowed in an
expression::
>>> f'{x+\
... 1}'
'2'
>>> d = {0: 'zero'}
>>> f'''{d[0
... ]}'''
'zero'
Format specifiers
-----------------
@ -374,7 +369,7 @@ or::
File "<stdin>", line 2, in <module>
ValueError: Sign not allowed in string format specifier
Leading and trailing whitespace in expressions is skipped
Leading and trailing whitespace in expressions is ignored
---------------------------------------------------------
For ease of readability, leading and trailing whitespace in