0

I am trying to use named expressions inside a f-string:

print(f"{(a:=5 + 6) = }")

Returns:

(a:=5 + 6) = 11

But I am hoping for something like this:

a = 11

Is that possible, by combining the walrus operator and f-strings (so that I don't have to declare the variable a first in a separate step)?

Gustav Rasmussen
  • 2,802
  • 3
  • 12
  • 33
  • 2
    The purpose of introducing the walrus operator was because in a few rare circumstances, its use can make code more readable. This is not one of them. Don't abuse it. – user2357112 supports Monica Jun 18 '20 at 07:08
  • So it is not recommended to use walrus operators inside f-strings in general? – Gustav Rasmussen Jun 18 '20 at 07:09
  • 1
    @dimay: That doesn't perform the assignment. The goal was to print *and* assign. – user2357112 supports Monica Jun 18 '20 at 07:11
  • @user2357112supportsMonica yes exactly. I was wondering if this is possible, and also if it was an anti-pattern – Gustav Rasmussen Jun 18 '20 at 07:14
  • I'd consider side effects inside an f-string a bad idea in general. It's much clearer to use two lines. – user2357112 supports Monica Jun 18 '20 at 07:15
  • Point taken. I much appreciate the feedback – Gustav Rasmussen Jun 18 '20 at 07:15
  • @GustavRasmussen the walrus operator assigns inside an expression, since there's nothing using `a` it's completely useless here. Likewise the `=` conversion sigil whose entire point is to print *the expression being f-printed* which is specifically *not* what you want here. Just use `print(f"a = {5 + 6}")`, it's shorter and does what you're asking for. Hell, you don't even need f-strings here, `print("a =", 5 + 6)` would do. – Masklinn Jun 18 '20 at 07:18
  • @Masklinn this is a small made up example, in the real code "a" is to be re-used – Gustav Rasmussen Jun 18 '20 at 07:19
  • 2
    I suppose you use could use `print(f"a = {(a:= 5 + 6)}")` – whackamadoodle3000 Jun 18 '20 at 07:19
  • @GustavRasmussen then it's fine to have it in the f-string, though you still won't have the formatting you're looking for with `=` (because that's not how it works). And it seems overcomplicated when assigning outside the f-string... actually does exactly what you want: `a = 5 + 6; print(f'{a=}')` and is the exact same length. I don't get why "(so that I don't have to declare the variable a first in a separate step)?" is a thing if you want `a` to be declared either way, and you *do not* want to print the entire declaration expression. – Masklinn Jun 18 '20 at 07:21
  • @whackamadoodle3000 nice, that works. It certainly answers my question that this is possible. I learned from user2357112 that it is not recommended practice though. – Gustav Rasmussen Jun 18 '20 at 07:22
  • @Masklinn yes I agree that separate assignment is more clear – Gustav Rasmussen Jun 18 '20 at 07:23

1 Answers1

0

If you don't care about having a afterwards, you can print(f"{5+6}")

Otherwise, I don't think it's possible without some complex tricks

marcant0
  • 146
  • 6