Live data from Hacker News

What’s New in Python 3.8

docs.python.org

261–270 of 381 posts

Re: What’s New in Python 3.8

#261
post #69

Earlier quoted context omitted.

> The "walrus operator" will occasionally be useful, but I doubt I will find many effective uses for it. The primary one I want is if m := re.match(...): print(m.group(1)) and while s := network_service.read(): process(s) both of which are both clearer and less error-prone than their non-walrus variants. The other one that I would have found useful an hour ago is in interactive exploration with comprehensions. I freq…

Coming from Perl, I used to want this badly, but then I thought that there's absolutely nothing wrong with m = re.match(...) if m is not None: pass Now, I wonder what you meant saying that single-line version is less error-prone, because I don't think so. I believe they're exactly the same in this regard, except for a bizarre case when someone would bastardize the code by putting some irrelevant lines between the ass…

Think about if you match multiple patterns:

    for line in f:
      if (m:= pat1.search(line)) is not None:
        ... do stuff ..
      elif (m:= pat2.search(line)) is not None:
        ... do other stuff ..
      elif (m:= pat3.search(line)) is not None:
         ... do something else ..
In older Python that's:

    for line in f:
      m = pat1.search(line)
      if m is not None:
        ... do stuff ..
      else:
        m = pat2.search(line)
        if m is not None:
          ... do other stuff ..
        else:
           m = pat3.search(line)
           if m is not None:
             ... do something else ..
I think the newer makes it clear that it's supposed to be a simple elif chain, where all branches following the same structure.

There are other ways to structure it, but the alternatives I can think of also have their own cumbersome complexities.

Re: What’s New in Python 3.8

#262
post #69
post #22

As a developer who has primarily developed applications in Python for his entire professional career, I can't say I'm especially excited about any of the "headlining" features of 3.8. The "walrus operator" will occasionally be useful, but I doubt I will find many effective uses for it. Same with the forced positional/keyword arguments and the "self-documenting" f-string expressions. Even when they have a use, it's us…

> The "walrus operator" will occasionally be useful, but I doubt I will find many effective uses for it. The primary one I want is if m := re.match(...): print(m.group(1)) and while s := network_service.read(): process(s) both of which are both clearer and less error-prone than their non-walrus variants. The other one that I would have found useful an hour ago is in interactive exploration with comprehensions. I freq…

Looking at your wish for exploratory evaluations in for-comprehensions, I was reminded of how Clojure does this. I found it quite elegant.

The inside of the for-braces allow for keyword-based sub-clauses: There's

- ":when" which will suppress output for elements not matching a filter expression,

- ":while" which will stop when elements stop matching a filter, and

- ":let" which will let you bind some new values in mid-loop.

I rarely need these features, but when I do I find them really really helpful. Maybe someday someone will consider doing something similar in Python.

Re: What’s New in Python 3.8

#263
post #112
post #69

Earlier quoted context omitted.

> The "walrus operator" will occasionally be useful, but I doubt I will find many effective uses for it. The primary one I want is if m := re.match(...): print(m.group(1)) and while s := network_service.read(): process(s) both of which are both clearer and less error-prone than their non-walrus variants. The other one that I would have found useful an hour ago is in interactive exploration with comprehensions. I freq…

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.

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

#264
post #82

Earlier quoted context omitted.

I you care about performance, maybe you should try pypy (the alternative python compiler)

I try PyPy every 3 months. It has improved greatly and for some periods I migrated to it. But for this particular project, most of time is spent inside lxml, pandas, scikit-learn and other extensions. CPython is actually faster than PyPy for this project. Maybe GraalVM / GraalPython can improve on this use-case.

Like, if graalpython gets ever released.

Re: What’s New in Python 3.8

#265
post #224

Earlier quoted context omitted.

That would of course be an absolute disaster, given that any user input could easily leak internal state and/or break your program.

I meant for inline strings (i.e., typed by the programmer). This is an inoffensive change. The only possible ``accident'' is when the programmer wants to write "{x}" instead of the value of x. This is such and exceptional case that it may be best treated by forcing to escape the curly brackets. If anything, user-input strings must be treated as tainted whatever the case.

That would break any existing strings that use curly braces, including most legacy use of str.format and docstrings that include code examples with dictionaries. It would be a big backward compatibility issue.

Re: What’s New in Python 3.8

#267
post #224

Earlier quoted context omitted.

That would of course be an absolute disaster, given that any user input could easily leak internal state and/or break your program.

I meant for inline strings (i.e., typed by the programmer). This is an inoffensive change. The only possible ``accident'' is when the programmer wants to write "{x}" instead of the value of x. This is such and exceptional case that it may be best treated by forcing to escape the curly brackets. If anything, user-input strings must be treated as tainted whatever the case.

A quick look at the Python stdlib gives shows some of the breakage that would occur:

1) existing uses of .format() would break:

   aifc.py: raise Error('marker {0!r} does not exist'.format(id))
2) existing uses of "%" formatting would break:

    argparse.py: result = '{%s}' % ','.join(choice_strs)
3) many regular expressions would break:

    uuid.py: if re.fullmatch('(?:[0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
4) existing strings which contain uuids would break:

    uuid.py: >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

Re: What’s New in Python 3.8

#268
post #44
post #30

def f(a, b, /, c, d, *, e, f): Wow: IMHO that is a very ugly syntax to define a fn with 6 parameters, although it looks to be a path dependency on previous decisions (*, and CPython ,/) and clearly there was much discussion of the need for the functionality and the compromises: https://www.python.org/dev/peps/pep-0570/ It amazes me to see how certain features make it into languages via community/committee (octal numb…

Yes, "path dependence" is a good way to describe it. (And Python has been my favorite language for 16+ years now) For https://www.oilshell.org/ , which has Python/JS-like functions, I chose to use Julia's function signature design, which is as expressive as Python's, but significantly simpler in both syntax and implementation: Manual: https://docs.julialang.org/en/v1/manual/functions/index.html... Comparison: https:/…

But the Julia syntax can not represent arguments that can be passed either positional or named, right?

Re: What’s New in Python 3.8

#269

This got missed from the release announcement, but now there's `functools.singledispatchmethod`,[1] as the class method sibling to `functools.singledispatch`.[2] This allows you to overload the implementation of a function (and now a method) based on the type of its first argument. This saves you writing code like: def foo(bar): if isinstance(bar, Quux): # Treat bar as a Quux elif isinstance(bar, Xyzzy): # Treat bar…

I can definitely imagine some places where this replaces type-checking, but it still seems like a bit of an unfortunate anti-pattern to me, since it's really a sort of C/C++ style function prototype match. My immediate thought is that it's going to be hard for PyCharm to reliably point me to a function definition.

>> My immediate thought is that it's going to be hard for PyCharm to reliably point me to a function definition.

I am certain PyCharm is going to special-case these decorators in their next release.

Re: What’s New in Python 3.8

#270

I stepped away from Python for about a year, and now I'm coming back to it. I hardly recognize the language. I'm not happy about this at all. I don't really have a point, except that Python 3 feels like a moving target.

I feel the same way as you do. For me, which version of an interpreter I'm using should be the kind of issue I only need to worry when solving extremely specific, deep-level problems. Python 3+ breaks this pact too often for my taste.

Considering this f-string example taken from another announcement:

   f"Diameter {(diam := 2 * r)} gives circumference {math.pi * diam:.2f}"
This is valid Python 3.8, but it's not valid in Python 3.7 (no walrus operator). And removing the walrus operator still doesn't work in Python 3.5 (no f-strings). On top of that, other comments already mention how f-strings have lots of weird corner cases anyway.

The entire point of Python in my circle of friends was that it made programming easy. Instead, I feel more and more in need of those "It works in my machine!" stickers. And good luck solving these issues if you are not a full-time programmer...

Post reply on HN