News for hackers. Stuff that matters.
Re:PEP 572: Python assignment expressions has been accepted by stfwn on Tuesday July 03, 2018 @06:09AM (#17448946)
This immediately looks useful for things like:

    if foo := bar[baz]:
        bar[baz] += 1
        return foo
    else:
        bar[baz] = 1
        return 0
Where foo is a dict keeping track of multiple things, and a non-existing key (baz) is never an error but rather the start of a new count. Faster and more readable than

    if baz in list(bar.keys()):
    ....
Similar to Swift’s ‘if let’, it seems.
Re:PEP 572: Python assignment expressions has been accepted by eesmith on Tuesday July 03, 2018 @06:48AM (#17449074)
The place I see using it is in (quoting Python's "python.exe-gdb.py"):

        m = re.match(r'\s*(\d+)\s*', args)
        if m:
            start = int(m.group(0))
            end = start + 10

        m = re.match(r'\s*(\d+)\s*,\s*(\d+)\s*', args)
        if m:
            start, end = map(int, m.groups())
With the new syntax this becomes:

        if m := re.match(r'\s*(\d+)\s*', args):
            start = int(m.group(0))
            end = start + 10

        if m := re.match(r'\s*(\d+)\s*,\s*(\d+)\s*', args)
            start, end = map(int, m.groups())
This pattern occurs just often enough to be a nuisance. For another example drawn from the standard library, here's modified code from "platform.py"

    # Parse the first line
    if (m := _lsb_release_version.match(firstline)) is not None:
        # LSB format: "distro release x.x (codename)"
        return tuple(m.groups())

    # Pre-LSB format: "distro x.x (codename)"
    if (m := _release_version.match(firstline)) is not None:
        return tuple(m.groups())

    # Unknown format... take the first two words
    if l := firstline.strip().split():
        version = l[0]
        if len(l) > 1:
            id = l[1]
Re:PEP 572: Python assignment expressions has been accepted by est on Tuesday July 03, 2018 @09:14PM (#17454726)
It' a problem with re module really.

re.match should return a match object no matter what, and .group() should return strings, empty string if non were matched.

Re:PEP 572: Python assignment expressions has been accepted by eesmith on Wednesday July 04, 2018 @12:44AM (#17455316)
I don't see how that would improve things. Could you sketch a solution based around your ideas?
Re:PEP 572: Python assignment expressions has been accepted by sametmax on Tuesday July 03, 2018 @06:42AM (#17449051)
Don't wait for 3.8, and don't bother with defaultdict.

collections.Counter is what you want for the counting case.

dict.get() + dict.setdefault() for the general case.

defaultdict is only useful if the factory is expensive to call.

Re:PEP 572: Python assignment expressions has been accepted by antoinealb on Tuesday July 03, 2018 @06:26AM (#17449008)
As pointed, you can use either a default dict or just simply, and [more pythonic](https://blogs.msdn.microsoft.com/pythonengineering/2016/06/2...):

    try:
      bar[baz] += 1
    except KeyError:
      bar[baz] = 1
Also you can check if a key is in a dict simply by doing "if baz in bar" no need for "list(bar.keys())", which will be slow (temp object + linear scan) vs O(1) hashmap lookup.
Re:PEP 572: Python assignment expressions has been accepted by stfwn on Tuesday July 03, 2018 @06:48AM (#17449075)
The error-catching method seemed too drastic to me before, but the article explains the LBYL vs. EAFP arugument quite well. Thanks!

I should find a way to get more code reviews, I really enjoy learning these small nuggets of info.

Re:PEP 572: Python assignment expressions has been accepted by bocklund on Tuesday July 03, 2018 @07:57AM (#17449409)
Alternatively

`bar[baz] = bar.get(baz, 0) + 1`

One line and no error checking.

But the OP was probably just illustrating a basic example where you might have some more intense logic

Re:PEP 572: Python assignment expressions has been accepted by bb88 on Tuesday July 03, 2018 @07:08AM (#17449181)
It's also time saving since the hash lookup needs to be done at most 1, as well. GP has two lookups in the hash list.
Re:PEP 572: Python assignment expressions has been accepted by kelnos on Tuesday July 03, 2018 @06:16AM (#17448968)
For stuff like that I'd just use `defaultdict`. That if/else tree then reduces to 2 lines total.
Re:PEP 572: Python assignment expressions has been accepted by stfwn on Tuesday July 03, 2018 @06:22AM (#17448988)
That’s a good tip, thanks!