[Python-checkins] peps: Incorporate feedback from Guido. Still several more items to tackle.

eric.smith python-checkins at python.org
Fri Aug 21 03:14:41 CEST 2015


https://hg.python.org/peps/rev/ae7fed88f7dd
changeset:   5962:ae7fed88f7dd
user:        Eric V. Smith <eric at trueblade.com>
date:        Thu Aug 20 21:14:43 2015 -0400
summary:
  Incorporate feedback from Guido. Still several more items to tackle.

files:
  pep-0498.txt |  31 +++++++++++++++++++++++++++----
  1 files changed, 27 insertions(+), 4 deletions(-)


diff --git a/pep-0498.txt b/pep-0498.txt
--- a/pep-0498.txt
+++ b/pep-0498.txt
@@ -251,6 +251,27 @@
   >>> '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 @@
 
   >>> 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 @@
 .. [#] 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)
 

-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list