News for hackers. Stuff that matters.
Re:Fstrings.wtf by pansa2 on Saturday July 19, 2025 @11:42AM (#44616460)
Despite the URL, I’d only consider a few of these to be WTFs. Questions 20 & 21 definitely are, though:

  >>> a = 42
  >>> print(f"{a:=10}")
          42
  >>> print(f"{(a:=10)}")
  10
I still can’t believe anyone thought the walrus operator was a good idea.
Re:Fstrings.wtf by almostgotcaught on Saturday July 19, 2025 @11:48AM (#44616509)
this has nothing to do with the walrus operator and everything to do with how string.format works in python https://docs.python.org/3/library/string.html#format-string-...
Re:Fstrings.wtf by lyu07282 on Saturday July 19, 2025 @01:18PM (#44617365)
They obviously meant the second example:

     >>> print(f"{(a:=10)}")
Re:Fstrings.wtf by almostgotcaught on Saturday July 19, 2025 @01:22PM (#44617395)
the output of the second example is "obvious" so i don't see the problem.
Re:Fstrings.wtf by pansa2 on Saturday July 19, 2025 @11:42PM (#44621793)
Neither of the examples is problematic on its own; it's the fact that the language supports both of them. They're very hard to distinguish at-a-glance yet behave entirely differently.
Re:Fstrings.wtf by sfink on Saturday July 19, 2025 @02:05PM (#44617820)
You can pry the walrus operator from my cold dead hands.

    m = re.match(pattern1, line)
    if m:
        do_stuff(m.group(1))
    else:
        m = re.match(pattern2, line)
        if m:
            do_other_stuff(m.group(2))
        else:
            m = re.match(pattern3, line)
            if m:
                do_things(m.groups())
            else:
                m = ...
Obviously, there are ways to improve this particular example without using the walrus. But it's also usually not this simple.

I run into something like this all the time. Not many times per program, and I don't actually use the walrus all that often, but when it's the right thing it's so very nice.

Re:Fstrings.wtf by marcosdumay on Saturday July 19, 2025 @03:56PM (#44618799)
You know, creating functions is allowed in Python. Are are loops.

The assignment is the least problematic thing on that code. I do even disagree on it being a problem.

Re:Fstrings.wtf by callc on Saturday July 19, 2025 @05:45PM (#44619715)
Even though I’ve never used python walrus, I’m a huge fan of the “X language feature can be used as an expression”

I feel the same way with rust control flow being used as an expression, and am disappointed when other languages don’t have it.

Even though one could argue it makes things more complicated, I feel more mental freedom and feel the language getting out of the way.

Re:Fstrings.wtf by pansa2 on Saturday July 19, 2025 @11:38PM (#44621775)
The walrus operator hardly changes that example at all:

    if m := re.match(pattern1, line):
        do_stuff(m.group(1))
    else:
        if m := re.match(pattern2, line):
            do_other_stuff(m.group(2))
        else:
            if m := re.match(pattern3, line):
                do_things(m.groups())
            else:
                m = ...
I've found `:=` useful in the headers of `while` loops, but that's about it. The complexity that walrus adds to the language far outweighs its usefulness.
Re:Fstrings.wtf by ghewgill on Sunday July 20, 2025 @12:29AM (#44621982)
The walrus operator allows you to use `elif` there to avoid cascading indentation.

    if m := re.match(pattern1, line):
        do_stuff(m.group(1))
    elif m := re.match(pattern2, line):
        do_other_stuff(m.group(2))
    elif m := re.match(pattern3, line):
        do_things(m.groups())
    else:
        ...
Re:Fstrings.wtf by sfink on Sunday July 20, 2025 @12:51AM (#44622081)
...and for me, the benefit is that this is much more clear to me when reading the code. An increasing level of indentation directly maps to needing to keep more in my head. (Sure, in this case you can pattern match and see that there's nothing else, but in general you need to watch out for cases where the else may be doing more than just the next check on the same inputs. )

But the walrus is good even in the absence of a series of matches. It is quite common to need to know more than the boolean "did this match or not?", and so to my eye it is much cleaner to have one line doing the test and the next lines using the results. I don't care about saving characters or even lines. `m = ...match...` immediately followed by `if m:` feels like overhead for the sake of the programming language's limitations; it's extraneous to what I want to express.

Also, I assert that the pattern is better for correctness. It's like Rust's `if let Some(foo) = f() { ... }` pattern: guard on a certain condition, then use the results if that condition is true.

    v = f()
    if v:
        ...use v...
invites injecting code in between the assignment and the test. They're two different things to the reader. And it's not as clear that `v` should only be used in the consequent body.

    if v := f():
        ...use v...
says exactly what it means: `f()` may return a truthy value. If it does, do something with it.
Re:Fstrings.wtf by pansa2 on Sunday July 20, 2025 @12:38AM (#44622020)
Ah, I missed that. Thanks
Re:Fstrings.wtf by Veedrac on Sunday July 20, 2025 @02:38PM (#44628013)
FWIW another way to handle this is a local function with early returns. In this example you can also just use a single regex and most likely win performance too.

:= is so common in other languages, or even other Python statements, that it doesn't bother me at all, but Python is such an imperative-first language that its presence is rarely felt.