Live data from Hacker News

What’s New in Python 3.8

docs.python.org

281–290 of 381 posts

Re: What’s New in Python 3.8

#281
post #115
post #112

Earlier quoted context omitted.

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.

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).

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

#282
post #235
post #227

Earlier quoted context omitted.

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.

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.

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

#283

Earlier quoted context omitted.

https://docs.python.org/3.0/library/bisect.html > This module provides support for maintaining a list in sorted order without having to sort the list after each insertion.

Problem with bisect is that bisect.insort() insertion is O(n), whereas C++ set.insert() is O(log n).

It is somewhat strange that Python does not have a binary tree in the standard library. I also couldn't find any discussion on the topic either. It might be a nice contribution.

Edit: it was coined a few times on the python-ideas mailing list but it seems it just died a silent death there. https://mail.python.org/archives/list/python-ideas@python.or...

Re: What’s New in Python 3.8

#284

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…

The issue with this and `singledispatch` is that they no longer support pseudo-types from the `typing` module [1] so you can't use them with containers of type `x`, e.g. `List[str]`, or protocols like `Sequence`.

[1] https://bugs.python.org/issue34498

Re: What’s New in Python 3.8

#286
post #260

Earlier quoted context omitted.

I think that feature has exactly zero chances of getting adopted, but that's probably one of my top Python annoyances too. If it's any consolation, the scope of a variable isn't determined by heuristics, it's just that the rules are (IMO) kinda bad. Basically, if a variable is assigned (in addition to `=` and friends, `import`, `class` and `def` are also assignments in disguise) to in what Python calls a "block" (whi…

Yes, I meant the rules are bad. I called them heuristics, because I assume they came up with the rules as a heuristic to what would be the most useful behaviour. Python didn't use to have lexical scoping, so the rules are retrofitting around that.

Yeah, it all seems a bit hacky. Especially the bit where a caught exception bound to a variable is cleared at the end of the `except` clause. I'm pretty sure it's because if you didn't you'd get a reference cycle (exception object -> stack trace -> function invocation record -> function locals -> exception object), which makes CPython's GC sad.

Re: What’s New in Python 3.8

#287
post #133

Earlier quoted context omitted.

> 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. Python is a fairly old, mature language. What features would you have been especially excited about?

I would love if they added "?" to the list of valid characters for names, so I could write something like `valid?()` instead of ``is_valid()`.

Or even better, just `valid?` Oh, wait .... that's Ruby.

Re: What’s New in Python 3.8

#288
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.

Then you would run into the same problem Kotlin has with its raw strings – the brackets need to be escaped, but raw strings disable escaping, so you'd have to write r"{}" as r"{'\{\}'}" which is quite ugly and not very raw at all.

Re: What’s New in Python 3.8

#289
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…

So Python is finally waking-up to the advantage of everything being an expression as in Ruby and Clojure.

Re: What’s New in Python 3.8

#290
post #133

Earlier quoted context omitted.

I would love if they added "?" to the list of valid characters for names, so I could write something like `valid?()` instead of ``is_valid()`.

just use "_P", a fairly-widely-recognized substitute from the Lisp world.

The suffix is just "p" in Common Lisp[1], so I think the "predicate" explanation is more likely to be correct than the visual analogy to the question mark.

[1] http://www.lispworks.com/documentation/HyperSpec/Body/f_list...

Post reply on HN