What's Coming in Python 3.8
331–340 of 558 posts
Re: What's Coming in Python 3.8
#332Earlier quoted context omitted.
>Read it "if y" - that's what's being tested. Once way to test if this works is to take the code, read it aloud, and then use the read-aloud version to rewrite the code. If you don't have a high degree of certainty that you end up with the same code, something has failed along the way. In this case, if I take "if x:= y()" and read it aloud as "if y", I think the vast majority of people would translate that to code as…
You will end up with the same code under two conditions: 1. You read more than one line of code. 2. Executing the code in the conditional more than once doesn't matter. If you meet those two assumptions, then the reading I suggested will transform this if x := y(): act_on(x) into this if y(): act_on(y()) which is, in fact, the same thing.
Even if y isn't mutating itself, it may be calling from a database updated from another thread.
You end up storing the value. The walrus simplifies the code.
Re: What's Coming in Python 3.8
#333Earlier quoted context omitted.
I truly wish this would become a thing. It's really frustrating having to update my installed packages and my code for some stupid change the language designers thought is sooo worth it. Just stabilize the bloody thing so I can do some work. Updating code so it meshes with the "latest and greatest" is _not real work_.
Fixing the entirely broken string/bytes mess up in Python 2 was worth it by itself. For bonus points old style classes went away, and the language got a significant speed boost. And now it’s not going to die a slow death, choking on the past poor decisions it’s burdened with. Trivializing that by suggesting it was some offhand, unneeded solution to a problem that some dreamy “language designer” thought up is at best…
Re: What's Coming in Python 3.8
#334Earlier quoted context omitted.
You don't even need the locals() function to get into trouble: x = [1, 2, 3, 4] def foo(): x[0] += 3 # Okay def bar(): x += [3] # UnboundLocalError def qux(): x = [5, 6, 7, 8] # Binds a new `x`.
def bar(): x += [3] # UnboundLocalError This is an especially funky one. x.extend([3]) would be allowed. Presumably x += [3] is not because it expands to x = x + [3]... However, the += operator on lists works the same as extend(), i.e. it changes the list in-place.
0 LOAD_FAST 0 (x)
2 LOAD_CONST 1 (3)
4 INPLACE_ADD
6 STORE_FAST 0 (x)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
So INPLACE_ADD and STORE_FAST are essentially doing x = x.__iadd__([3])Re: What's Coming in Python 3.8
#335Earlier 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…
So from the perspective of Python as executable pseudocode it makes some sense.
Re: What's Coming in Python 3.8
#336Earlier quoted context omitted.
I'm 34 and I don't like this, so it's definitely not only those above 35. Jokes aside, I would say I'm a minimalist and this is where my resistance comes from. One of the things that I dislike the most in programming is feature creep. I prefer smaller languages. I like the idea of having a more minimal feature set that doesn't change very much. In a language with less features, you might have to write slightly more c…
> In a language with less features, you might have to write slightly more code, but the code you write will be more readable to everyone else. I disagree with this, which is precisely why I prefer feature rich languages like Java or better yet Kotlin. It doesn't get much more readable than something like: users.asSequence() .filter { it.lastName.startsWith("S") } .sortedBy { it.lastName } .take(3) Now try writing tha…
sorted((u for u in users
if u.last_name.startswith("S")),
key=lambda u: u.last_name
)[:3]
If last_name is a function, which it often would be in Python, it gets better: sorted((u for u in users
if last_name(u).startswith("S")),
key=last_name
)[:3]
However, I think you probably got the sort key wrong if you're taking the first three items of the result. Maybe you meant key=abuse_score, reverse=True, or something.Re: What's Coming in Python 3.8
#337Earlier quoted context omitted.
> In a language with less features, you might have to write slightly more code, but the code you write will be more readable to everyone else. I disagree with this, which is precisely why I prefer feature rich languages like Java or better yet Kotlin. It doesn't get much more readable than something like: users.asSequence() .filter { it.lastName.startsWith("S") } .sortedBy { it.lastName } .take(3) Now try writing tha…
A little off-topic but how does that work? Is 'it' a magic variable referring to the first argument? Never seen magic variables that blend into lambdas like that before... would've expected $1 or something like that.
Re: What's Coming in Python 3.8
#338Earlier quoted context omitted.
But now there are two ways to do assignment. That's not very pythonic, is it?
I never felt like there was only one way to do something in Python. Every Stack Overflow question has a multitude of answers ranging from imperative to functional style and with various benefits and drawbacks. Python is one of the least "only one way to do things" languages I've used. This even extends to its packaging system, where you can choose between virtualenv, pipenv, pyenv, etc. Same goes for the installation…
Re: What's Coming in Python 3.8
#339Earlier quoted context omitted.
In my experience, every technology focused on building a "simple" alternative to a long-established "complex" technology is doomed to discover exactly _why_ the other one became "complex." Also spawn at least five "simple" alternatives. Doesn't mean nothing good comes out of them, and if it's simplicity that motivates people then eh, I'll take it, but gosh darn the cycle is a bit grating by now.
Haha, what was that quote? Something like, any language is going to iterate towards a crappy version of lisp.
Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp. - Philip Greenspun
Re: What's Coming in Python 3.8
#340Earlier quoted context omitted.
I envisioned it like "if/else" or "for/else" or "while/else", where a "do" block must be followed by a "while" block. x = 0 do: x += 1 while: x
This completely contradicts the rest of Python grammar, and indeed many languages’ grammars. The consistent way would then be `while x < 10` but that too looks ridiculous. The issue is that you can’t have post-clause syntax in Python due to its infamous spacing-is-syntax idea.
do:
body()
body()
while x
It's just a compound statement consumes the trailing while clause.Decorators already precede a function (or class) definition[2], and one alternative for the ill-fated switch statement[1] was to have switch precede the case blocks to avoid excessive indentation.
So there's plenty of precedent in the other direction.
[1]: https://www.python.org/dev/peps/pep-3103/#alternative-3