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