Live data from Hacker News

What's Coming in Python 3.8

lwn.net

41–50 of 558 posts

Re: What's Coming in Python 3.8

#41

The walrus operator does not feel like Python to me. I'm not a big fan of these types of one liner statements where one line is doing more than one thing. It violates the philosophies of Python and UNIX where one function, or one line, should preferably only do one thing, and do it well. I get the idea behind the :=, but I do think it's an unnecessary addition to Python.

>It violates the philosophies of Python and UNIX where one function, or one line, should preferably only do one thing, and do it well.

Python never had that philosophy... You might confused it with "there should be one, and preferably only one, obvious way to do anything".

Re: What's Coming in Python 3.8

#42
post #24

Earlier quoted context omitted.

“I've come up with a set of rules that describe our reactions to technologies: 1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works. 2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. 3. Anything invented after you're thirty-five is against the n…

Does that mean someone born in 2008 will think C++ is simple and elegant?

No, the claim is that it's ordinary and just part of the way the world works.

Re: What's Coming in Python 3.8

#44
I recommend a talk from Pycon 2019, wherein Dustin Ingram explains PEP-572 (aka the Walrus Operator) better than I’ve seen done elsewhere.

IMHO, the usefulness of this new operator outweighs the slight learning curve required to get past the awkwardness you will experience when you are first acquainted to it.

Here is that talk:

https://youtu.be/6uAvHOKofws

Re: What's Coming in Python 3.8

#45

I'm all in favor of the walrus operator for the for loop, but the first example given to justify it is code I'd never write. The first if does a return, so there is no need for the else: and indentation. I'm sure there are other code examples that would justify it, but this one is unconvincing.

The return statements make it a poor example. There's an example from the standard library in the PEP that has a similar shape:

  reductor = dispatch_table.get(cls)
  if reductor:
      rv = reductor(x)
  else:
      reductor = getattr(x, "__reduce_ex__", None)
      if reductor:
          rv = reductor(4)
      else:
          reductor = getattr(x, "__reduce__", None)
          if reductor:
              rv = reductor()
          else:
              raise Error(
                  "un(deep)copyable object of type %s" % cls)
Becomes:

  if reductor := dispatch_table.get(cls):
      rv = reductor(x)
  elif reductor := getattr(x, "__reduce_ex__", None):
      rv = reductor(4)
  elif reductor := getattr(x, "__reduce__", None):
      rv = reductor()
  else:
      raise Error("un(deep)copyable object of type %s" % cls)

Re: What's Coming in Python 3.8

#46
post #25

Earlier quoted context omitted.

I love f-strings. I just wish tools like pylint would shut up when I pass f-strings to the logging module. I as the developer understand and accept the extra nanosecond of processor time to parse the string that might not be logged anywhere!

Disable it in pylintrc. Pylint is unusable without a good config file anyway.

Ideally the defaults should be sensible. I have found they mostly are, except the f-string one.

Re: What's Coming in Python 3.8

#47
post #8

Python looks more and more foreign with each release. I'm not sure what happened after 3.3 but it seems like the whole philosophy of "pythonic", emphasizing simplicity, readability and "only one straightforward way to do it" is rapidly disappearing.

I see what you're saying, but I kinda like the gets ":=" operator.

Re: What's Coming in Python 3.8

#48
Gotta ask how many of these changes are actually reflective of changing environments.

I could see with c++ that between 2003 and 2014 a fair few underlying machine things were changing and that needed addressing in the language.

But Python is not quite as close to the machine, and I don't see how something like the walrus is helping much. If anything it seems like you'd scratch your head when you came across it. And for me at least one of the main attractions of python is you're hardly ever surprised by anything, things that are there do what you guessed, even if you hadn't heard of them. Function decorators for instance, you might never have seen one but when you did you knew what it was for.

Same with the debug strings. That seems to be a special case of printing a string, why not leave it at that? I'm guessing a lot of people don't ever read a comprehensive python guide, what are they going to do when they see that?

Re: What's Coming in Python 3.8

#49
post #30
post #6

Without wanting to ignite a debate about the walrus operator (and having not read any of the arguments), I can guess why there was one. It's not clear to me what it does just from reading it, which was always one of Python's beginner-friendlinesses.

> It's not clear to me what it does just from reading it How isn't it entirely obvious? := is the assignment operator in tons of languages, and there's no reason not to have assignment be an expression (as is also the case in many languages).

It's not in a language I've ever used (furthermore, I explicitly mentioned beginners in my comment).

Re: What's Coming in Python 3.8

#50

I'm all in favor of the walrus operator for the for loop, but the first example given to justify it is code I'd never write. The first if does a return, so there is no need for the else: and indentation. I'm sure there are other code examples that would justify it, but this one is unconvincing.

Nice way in without walrus

    m = re.match(p1, line)
    if m:
        return m.group(1)
    m = re.match(p2, line)
    if m:
        return m.group(2)
    m = re.match(p3, line)
    ...
With walrus:

    if m := re.match(p1, line):
        return m.group(1)
    elif m := re.match(p2, line):
        return m.group(2)
    elif m := re.match(p3, line):
The example would have been better if it didn't have the return, but just a value assign or a function call.
Post reply on HN