Live data from Hacker News

What's Coming in Python 3.8

lwn.net

211–220 of 558 posts

Re: What's Coming in Python 3.8

#211

https://docs.python.org/3.8/whatsnew/3.8.html

I don't know why the downvotes, but I personally much prefer this to the editorialized and incomplete list in the current list.

Looking at the module changes, I think my top pick is the changes to the `math` module:

> Added new function math.dist() for computing Euclidean distance between two points.

> Added new function, math.prod(), as analogous function to sum() that returns the product of a ‘start’ value (default: 1) times an iterable of numbers.

> Added new function math.isqrt() for computing integer square roots.

All 3 are super useful "batteries" to have included.

Re: What's Coming in Python 3.8

#213

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.

The unix philosophy of simplicity was on a per tool basis, not function or line of code. The walrus operator is Python version of what we can do now in C or in JS, doing plain assignment in an expression while evaluating it for truthiness. And more often than not, the point of that single-purposeness in Unix is so you can chain a bunch of piped commands that result in a perl-like spaghetti command that's three terminal widths long.

Re: What's Coming in Python 3.8

#214

Anyone else think the walrus operator is just plain ugly? There is a certain aesthetic quality that I've always appreciated about the Python language and the walrus operator looks like something straight out of Perl or Shell.

It's also used in Algol, Pascal, Modula and other perfectly respectable languages.

Re: What's Coming in Python 3.8

#215
post #181

Earlier quoted context omitted.

f-strings are the first truly-pretty way to do string formatting in python, and the best thing is that they avoid all of the shortcomings of other interpolation syntaxes I've worked with. It's one of those magical features that just lets you do exactly what you want without putting any thought at all into it. Digression on the old way's shortcomings: Probably the most annoying thing about the old "format" syntax was…

> If `str.dedent` was a thing Have you looked at textwrap.dedent?

Yes! `textwrap.dedent` is great. On further reflection `wrap` is actually more useful for this kludge (see below). But my point is that that's a whole import for a kludge. Compare the f-string ideal (by my standards):

  raise ValueError("File exists, not uploading: "
                   f"{filename} -> {bucket}, {key}")
...which is short enough that it's readable, and it's clear where exactly each variable is going. It's the single obvious solution, so much so that I don't spend a second thinking about it (very Pythonic!). Compare it to using `str.format` with the same continued indentation:

  raise ValueError(("File exists, not uploading: {filename} -> "
                    "{bucket}, {key}").format(filename=filename,
                                              bucket=bucket,
                                              key=key))
Even this minimal example looks terrible! Remember that a lot of exceptions are raised within multiply-nested blocks, and then format pushes things farther to the right (while also ruining your automated string-literal concatenation, hence the extra parentheses), leaving very little room for the format arguments. You can use a more self-consistent and readable indentation strategy:

  raise ValueError(
      (
          "File exists, not uploading: {filename} -> "
          "{bucket}, {key}"
      ).format(filename, bucket, key)
  )
This is unquestionably more pleasant to read than the former, but it's 3 times longer than the simple f-string solution, and I would argue it is not any more readable than the f-string for this simple example. My point with having a `str.wrap` builtin is that at least you could use the docstring convention of terminating multi-line strings on a newline, which would get rid of the string concatenation issues while leaving you a consistent (albeit diminished by the "wrap" call) amount of rightward room for the `format` args:

  raise ValueError("""File exists, not uploading: {filename} ->
                   {bucket}, {key}
                   """.dedent().format(filename=filename,
                                       bucket=bucket, key=key))
Maybe a little bit better than the first one, especially if you're writing a longer docstring and don't want to think about string concatenation. But still a kludge. You can use positional formatting to shorten things up, but the fundamental weakness of `str.format` remains.

Re: What's Coming in Python 3.8

#216
post #118

Despite controversy, walrus operator is going to be like f-strings. Before: "Why do we need another way to..." After: "Hey this is great". People are wtf-ing a bit about the positional-only parameters, but I view that as just a consistency change. It's a way to write in pure Python something that was previously only possible to say using the C api.

Was the controversy really about the need for the feature? I thought most people agreed it was a great feature to have, and most of the arguments were about `:=` vs re-using `as` for the operator.

All discussion I've ever seen was about the need for the feature, not its spelling. I didn't even know "as" was proposed, but in fact it is an "alternate spelling" they considered[1] in the PEP.

[1] https://www.python.org/dev/peps/pep-0572/#alternative-spelli...

Re: What's Coming in Python 3.8

#217
post #191

Earlier quoted context omitted.

Which equals? = (existing) is statement assignment == (existing) is expression equality := (new) is expression assignment

Just 1 equals. It could assign a statement to a variable, and return that value/variable to the if statement to check for truthyness a=42 if b = a: print(b) else: print("no") Would print "42". It works in C int a,b; a=42; if(b=a){ printf("%d\n",b); } else { printf("no\n"); }

That works if “if” statements were the only place the assignment expression operator could be used.

It works less well if they can be used everywhere an expression can occur, including the right side of assignments—especially since Python has both multiple (x = y, z) and chained (x = y = z) assignment, which can be used together.

What does this mean if = is used for both assignment statements and assignment expressions:

  x = y, z = 10, 20
When they are distinct, these have different meaning:

  x = y, z = 10, 20  # x: (10, 20), y: 10, z: 20
  x = y, z := 10, 20 # x: (, (10, 20)), y: , z: (10, 20)

Re: What's Coming in Python 3.8

#218
post #191

Earlier quoted context omitted.

Which equals? = (existing) is statement assignment == (existing) is expression equality := (new) is expression assignment

Just 1 equals. It could assign a statement to a variable, and return that value/variable to the if statement to check for truthyness a=42 if b = a: print(b) else: print("no") Would print "42". It works in C int a,b; a=42; if(b=a){ printf("%d\n",b); } else { printf("no\n"); }

It works in C, and have caused countless bugs in C (and C++).

So much so that many have adopted the rule that the variable goes on the right, "if 42 = b", to make sure the compiler barfs when you intended to write "if b == 42".

With := it's less likely that mistake is made. I also find it visually more distinct, so easier to parse, but that might be very subjective.

Re: What's Coming in Python 3.8

#219

Earlier quoted context omitted.

All you're doing then is moving the evolution of the language into the common libraries, community conventions, and tooling. Think of JavaScript before ES2015: it had stayed almost unchanged for more than a decade, and as a result, knowing JavaScript meant knowing JS and jQuery, prototype, underscore, various promise libraries, AMD/commonjs/require based module systems, followed by an explosion of "transpiled to vani…

It's apples and oranges. Python was explicitly designed and had a dedicated BDFL for the vast majority of its nearly 30 year history functioning as a standards body. JS, on the other hand, was hacked together in a week in the mid-90s and then the baseline implementation that could be relied on was emergent behavior at best, anarchy at worst for 15 years.

Agreed, but the anarchy of JS was a result of a dead standards process between the major vendors that resulted in de facto freeze. The anarchy is direct result of a stewardship body not evolving the language to meet evolving needs.

Re: What's Coming in Python 3.8

#220
post #18
post #5

Walrus operator looks like a great addition, not too much syntax sugar for a common pattern. Why were folks arguing about it?

I write a good deal of python and I can't think of a line of code that I would use it for besides the while loop on a non-iterable data source, which is such a once in a blue moon case. As mentioned by others, the operator invites more ways to do the same thing, which is not what Python has been viewed as being about.

I do this all the time:

x = function_that_might_return_none()

if x: do_stuff

Post reply on HN