OSDir

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] PEP 572 and f-strings


On Sat, May 12, 2018 at 9:11 PM, Eric V. Smith <eric at trueblade.com> wrote:
> I don't think it matters to its acceptance, but PEP 572 should at least
> mention that the := syntax means that you cannot use assignment expressions
> in f-strings.
>
> As I wrote in a python-ideas email, f'{x:=4}' already has a defined meaning
> (even if no one is using it).

As with lambda functions, you can't write them the simple way.
However, you can parenthesize it, and then it works fine.

>>> f"@{(lambda: 42)}@"
'@<function <lambda> at 0x7f09e18c4268>@'
>>> f"@{(y := 1)}@"
'@1@'
>>> y
1

ChrisA