Emily Morehouse <[email protected]> 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 <[email protected]>
<https://bugs.python.org/issue36798>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com