News for hackers. Stuff that matters.
Re:What’s New in Python 3.8 by 3PS on Monday October 14, 2019 @08:31PM (#21254396)
The issue, in my opinion, is when you want something like this:

    while m := f(many, args):
        # do stuff with m
Now, if you're writing this in Python 3.7, you often end up with some code duplication:

    m = f(many, args)
    while m:
        # do stuff with m
        m = f(many, args) # duplicate
Or something like this:

    while True:
        m = f(many, args)
        if not m:
            break
        # do stuff with m
Personally, I consider the last version to be the most elegant, but still something of an anti-pattern. So I welcome this new operator.
Re:What’s New in Python 3.8 by hermitdev on Monday October 14, 2019 @09:41PM (#21254814)
I literally wrote your last example twice last week to hash files in 4k chunks. Those 4 lines would be reduced to 1 with the walrus operator. I welcome it, as well.

Edit: 1 line, not 2 (excluding the "do stuff")

Re:What’s New in Python 3.8 by cellularmitosis on Tuesday October 15, 2019 @12:07PM (#21260013)
This comes up in recursive descent implementations as well https://gist.github.com/cellularmitosis/53913ad229bad5d0a3bb...
Re:What’s New in Python 3.8 by ygrechuk on Monday October 14, 2019 @10:22PM (#21255033)
Why not something like this:

  def f_iter(many, args):
      while True:
          m = f(many, args)
          if m:
              yield m
          else:
              raise StopIteration
  ...
  for m in f_iter(many, args):
      # do stuff with m
This way you’re isolating all the initialization logic, error handling, etc. And you can focus on your domain logic in your client code.
Re:What’s New in Python 3.8 by bvrmn on Tuesday October 15, 2019 @10:54AM (#21259059)
Python has built-in facilities for that, there is no need to write wrapper by yourself.

   from functools import partial
   with open('mydata.db', 'rb') as f:
       for block in iter(partial(f.read, 64), b''):
           process_block(block)
Re:What’s New in Python 3.8 by ciupicri on Tuesday October 15, 2019 @10:03AM (#21258420)
Instead of raising StopIteration you could also just return from the function. You could also make it a bit shorter if you swap the if branches.

  def f_iter(many, args):
      while True:
          m = f(many, args)
          if not m:
              return
          yield m
Re:What’s New in Python 3.8 by zb on Tuesday October 15, 2019 @10:12AM (#21258523)
Instead of raising StopIteration you have to return from the function, otherwise you'll just get a RuntimeError starting with Python 3.7.
Re:What’s New in Python 3.8 by ciupicri on Tuesday October 15, 2019 @11:20AM (#21259375)
You're right. The [changelog] for Python 3.7 states:

> [bpo-32670]: Enforce [PEP 479] for all code. This means that manually raising a StopIteration exception from a generator is prohibited for all code, regardless of whether ‘from __future__ import generator_stop’ was used or not.

[changelog]: https://docs.python.org/3.7/whatsnew/changelog.html#id115

[bpo-32670]: https://bugs.python.org/issue32670

[PEP 479]: https://www.python.org/dev/peps/pep-0479/

Re:What’s New in Python 3.8 by repsilat on Monday October 14, 2019 @10:37PM (#21255107)
And less reasonably, something similar for regular expressions:

    for m in filter([re.match(...)], bool):
        ...
;-)
Re:What’s New in Python 3.8 by mlevental on Tuesday October 15, 2019 @12:03AM (#21255459)
so i should write a whole other function that iterates over a list instead of being happy that := exists?
Re:What’s New in Python 3.8 by throwaway744678 on Tuesday October 15, 2019 @03:47AM (#21256368)
Yes! No pain, no gain!
Re:What’s New in Python 3.8 by pyrale on Tuesday October 15, 2019 @03:51AM (#21256383)
The last version clearly exposes that you have an infinite loop. This is something hidden by the `while(evaluate)` expression.

Also if it comes to pure form I would have preferred `while (evaluate) as x:` out of establishing a parallel with existing syntax, but that's not very important.

Re:What’s New in Python 3.8 by pdpi on Tuesday October 15, 2019 @04:34AM (#21256590)
On the contrary: the loop isn’t fundamentally infinite, it’s just an artefact of the language that you have to write it that way. The walrus lets you put the termination where it belongs.
Re:What’s New in Python 3.8 by pyrale on Tuesday October 15, 2019 @07:44AM (#21257489)
I don't understand that the walrus has any significance with regards to the `while` loop termination. it's the nature of the evaluated expression that makes a difference, more precisely, whether it will return something that is cast to False.

In that sense, while loops over expressions always require knowledge of the expression to understand the semantic. On the other hand, something like:

    while True:
       with x as y:
           print(y)
would not require understanding of x to understand the intent. Therefore it is not semantically equivalent to:

    while x as y:
        print(y)
Note that in both cases, `:=` is strictly equivalent to `as` as it is defined for `with`. That is a matter of personal preference.
Re:What’s New in Python 3.8 by xapata on Tuesday October 15, 2019 @01:43AM (#21255840)
That last one isn't an anti-pattern. That's the pattern concluded best for do-while in the discussion of PEP-315.

https://www.python.org/dev/peps/pep-0315/#notice