News for hackers. Stuff that matters.
Re:What’s New in Python 3.8 by est on Monday October 14, 2019 @09:32PM (#21254765)
IMHO the whole `re.match` is a design flaw in stdlib.

re.match() should always return a match object, but instead .group(1) will return None. Then we can write one-liners easier without the walrus operator.

Re:What’s New in Python 3.8 by nine_k on Monday October 14, 2019 @09:45PM (#21254833)
Sometimes you just check for a match (e.g.of entire string) and define no groups.

The problem is not in re.match but in None being a relatively poor Maybe / Optional (still ways better than C-style `null` of many other languages).

Re:What’s New in Python 3.8 by geofft on Tuesday October 15, 2019 @07:36AM (#21257455)
Could just have been

    if re.match(...).matched:
But yeah, agree that Optional is what I want here.

    if let Some(m) := re.match(...)
        m.group(1)...

    m = re.match(...)
    m.group(1) # TypeError because I haven't unwrapped the Optional
In current Python, that last line sometimes throws a runtime error.
Re:What’s New in Python 3.8 by est on Monday October 14, 2019 @09:46PM (#21254836)
> Sometimes you just check for a match

re.match vs re.search

Re:What’s New in Python 3.8 by Liquid_Fire on Tuesday October 15, 2019 @07:23AM (#21257389)
re.search is different. re.match matches from the beginning of the string, while re.search matches from anywhere in the string.
Re:What’s New in Python 3.8 by eesmith on Tuesday October 15, 2019 @06:32AM (#21257162)
I think you mean .group(0) as .group(1) can already return None:

    >>> import re
    >>> pat = re.compile("(A+)|(B+)")
    >>> m = pat.search("HUBBLE")
    >>> m.group(1) is None
    True
    >>> m.group(2)
    'BB'
    >>> m.group(0)
    'BB'
I don't see how using your proposed change would help.
Re:What’s New in Python 3.8 by est on Wednesday October 16, 2019 @01:08AM (#21267239)
I mean re.search() should always return a MatchObject, dont return None.