[issue36798] f-strings do not support top-level :=

Emily Morehouse report at bugs.python.org
Sun May 5 11:42:39 EDT 2019


Emily Morehouse <emilyemorehouse at gmail.com> added the comment:

My initial reaction is that named expressions should not be valid in f-strings and should instead raise an exception, the same way that using `a := 10` does.

>>> a := 10
  File "<stdin>", line 1
    a := 10
      ^
SyntaxError: invalid syntax

It could be expected that named expressions could be used in f-strings in the same way as, say, list comprehensions or when used in parenthesis. One of the tricky things about named expressions is that the scope of the variable being assigned to gets "elevated" to the enclosing scope (this is a slightly simplified explanation but applies for most cases).

Since f-strings are executed when defined and not where they are used, this could lead to confusing behavior. Is it available only when defined? Would users expect a variable to be available again when the f-string if it were saved to a variable? Would we modify the expected behavior of named expressions to contain scope to only the f-string? I'm not sure that any of these are particularly clear in behavior or in the definitions laid out in PEP 572.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36798>
_______________________________________


More information about the Python-bugs-list mailing list