From 2f170cfcaf65a1e78fe15bc4990dd5cec64bc884 Mon Sep 17 00:00:00 2001 From: "Eric V. Smith" Date: Fri, 4 Sep 2015 17:09:56 -0400 Subject: [PATCH] Add notion of adding implicit parens before parsing the expression. This allows for newlines in the expressions. --- pep-0498.txt | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/pep-0498.txt b/pep-0498.txt index 1b9f39c9a..69d74d42e 100644 --- a/pep-0498.txt +++ b/pep-0498.txt @@ -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, -'', 'eval')`` [#]_. Note that this restricts the expression: -it cannot contain any newlines, for example:: +Expressions are parsed with the equivalent of ``ast.parse('(' + +expression + ')', '', '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 "", 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 "", line 2, in 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