Live data from Hacker News

What's Coming in Python 3.8

lwn.net

71–80 of 558 posts

Re: What's Coming in Python 3.8

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

> := is the assignment operator in tons of languages

It is? Which ones? Other than Go, I can not think of a single language that has ":=" as an operator. Java does not, JavaScript does not, C/C++ do not, Ruby does not, I don't think PHP does, Erlang/Elixir do not, Rust does not... (I could be wrong on these, but I've personally never seen it in any of these languages and I can't find any mention of it in these languages' docs).

I tried looking around the internet at various popular programming languages and the only ones I could find that use ":=" are: Pascal, Haskell (but it's used for something else than what Python uses it for), Perl (also used for something else), and Scala (but in Scala it isn't officially documented and doesn't have an 'official' use case).

I don't have a strong opinion about ":=" in Python but I do agree that it's unintuitive and thus not very "Pythonic".

Re: What's Coming in Python 3.8

#72
post #46

Earlier quoted context omitted.

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.

For something as finely tuned as pylint, my sensible default and your would never be the same.

I don't want it to scream on a missing docstring for every single thing. I do want to be able to use a, b and x as variables. No, this root var is not a constant, don't ask me for uppercase. Etc.

Re: What's Coming in Python 3.8

#73
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 don't know how you read it - 'if x is assigned the value y'? Most other things in Python can just be read out loud.

Read it "if y" - that's what's being tested.

The walrus simultaneously names the value being tested so you can refer to it within the condition; it's sort of the inverse of Perl code using $_. So instead of

    if (do_something()) {
        act_on($_);
    }
you have

    if placeholder := do_something():
        act_on(placeholder)
But when reading aloud, however you'd read the perl will flow as more natural english. "If the string contains z, do something with it".

If you really want to read the Python as it's written, it corresponds to the second of these sentences:

- If the substring from 3 to 5 is "fg", crash.

- If variable_name, the substring from 3 to 5, is "fg", crash.

Re: What's Coming in Python 3.8

#74
post #62

Earlier quoted context omitted.

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

But now there are two ways to do assignment. That's not very pythonic, is it?

Regular = can only be used in statements. Walrus := can only be used in expressions. There's no overlap there. However, := does simplify certain expressions (like those nested if-else statements and the common "while chunk := read()" loop), which I think does justify its existence.

Re: What's Coming in Python 3.8

#75
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 looks to me like it could be an assignment to const, or, a copy vs a non-copy - it’s not obvious at all. I’m sure: ‘?=‘ was fought over and rejected, but that’s what I’d have expected conditional assignment to look like.

Re: What's Coming in Python 3.8

#77
post #62

Earlier quoted context omitted.

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

But now there are two ways to do assignment. That's not very pythonic, is it?

Assignment can be confusing already.

  >>> locals()['a'] = 1
  >>> a
  1
If anything, the walrus operator allows for tightly-scoped assignment, which is good in my opinion.

Re: What's Coming in Python 3.8

#78
post #62

Earlier quoted context omitted.

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

But now there are two ways to do assignment. That's not very pythonic, is it?

Not really, but neither are ugly nested if statements (Flat is better than nested, readability counts, etcetera). You need to make tradeoffs.

Maybe it would have been better to only have a single := assignment operator to begin with. But it's a few decades too late for that.

For what it's worth, := is for expressions only. Using it as a statement is a syntax error. So there won't be a lot of cases where both are equally elegant options.

Re: What's Coming in Python 3.8

#79
post #62

Earlier quoted context omitted.

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

But now there are two ways to do assignment. That's not very pythonic, is it?

This isn't really true. There's one way to do assignment, `=`, and one way to do special assignment that also works as an expression, `:=`. You should always use `=` unless you *need `:=`.

Re: What's Coming in Python 3.8

#80
post #30

Earlier quoted context omitted.

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

> := is the assignment operator in tons of languages It is? Which ones? Other than Go, I can not think of a single language that has ":=" as an operator. Java does not, JavaScript does not, C/C++ do not, Ruby does not, I don't think PHP does, Erlang/Elixir do not, Rust does not... (I could be wrong on these, but I've personally never seen it in any of these languages and I can't find any mention of it in these langua…

Oracle's PL/SQL uses it. I believe it was based on ADA... googles ... Yes ADA uses it as well.
Post reply on HN