Live data from Hacker News

What’s New in Python 3.8

docs.python.org

151–160 of 381 posts

Re: What’s New in Python 3.8

#151

This is the release that contains the controversial assignment expressions, which sparked the debate that convinced GvR to quit as BDFL. They didn't select my preferred syntax, but I'm still looking forward to using assignment expressions for testing my re.match() objects. I haven't used 3.8.0 yet but I hope its a good one because 2020 is the year that 2.7 dies and there will be a lot of people switching.

>They didn't select my preferred syntax I don't write much python so there's probably something obvious I'm missing, but I don't see why they didn't use "=". Is there some significant difference between assignment expressions and assignment statements that makes it worth having distinct syntax?

> Is there some significant difference between assignment expressions and assignment statements that makes it worth having distinct syntax?

Given that Python is statement-oriented, yes, having statements visually distinct from similar expressions is important.

It's also important to avoid making the equality operator and the assignment operator visually similar or easy to typo one for the other, which is arguably the bigger need for “:=” vs “=”, since “=” and “==” are quite similar and easy to accidentally mistype for each other.

Re: What’s New in Python 3.8

#152
post #69

Earlier quoted context omitted.

> The "walrus operator" will occasionally be useful, but I doubt I will find many effective uses for it. The primary one I want is if m := re.match(...): print(m.group(1)) and while s := network_service.read(): process(s) both of which are both clearer and less error-prone than their non-walrus variants. The other one that I would have found useful an hour ago is in interactive exploration with comprehensions. I freq…

Coming from Perl, I used to want this badly, but then I thought that there's absolutely nothing wrong with m = re.match(...) if m is not None: pass Now, I wonder what you meant saying that single-line version is less error-prone, because I don't think so. I believe they're exactly the same in this regard, except for a bizarre case when someone would bastardize the code by putting some irrelevant lines between the ass…

If you only have one or two regex like that then there is no difference in readability. If you have ten in a row though then suddenly your function no longer fits on the screen and it’s more difficult to understand what’s going on.

Re: What’s New in Python 3.8

#153
post #74

Earlier quoted context omitted.

Yes, there is. If x = 1 Has been the source of many errors in many languages. Forcing assignment to be more than a 1 character difference from the equality operator prevents this.

I may be missing something, but := is still only a 1 character difference, isn't it?

It depends on the definition of edit distance used; they both have a unweighted Levenshtein distance of 1 from “==” (# of inserts, deletions, or substitions), but “:=” has an LCS distance of 2 vs “=” with 1.

Perhaps more importantly, “=” and “==” are more visually similar than “==” and “:=” and also easier to mistakenly type for each other.

Re: What’s New in Python 3.8

#154
post #71

Earlier quoted context omitted.

Perl devs have been using the walrus expression for decades, but we just called it "assigning a variable with local scope in an expression". $ perl $a = "foo"; if ( my $a = "bar" ) { print "$a\n" } print "$a\n" bar foo

Yup, C and many other languages also have it: if (ptr = strchr(address, ':')) port = atoi(ptr + 1);

It doesn't do scoping, so not really the same.

Re: What’s New in Python 3.8

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

PEP-3136 was proposed and discussed more than 10 years ago in the 2->3 process. The python community was very different then. As an example, Python 3 was also the language that removed the function argument tuple packing, to which my reaction is basically WTF.

Re: What’s New in Python 3.8

#156

> In this example, the assignment expression helps avoid calling len() twice: if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected Um, no it doesnt?: a1 = [10, 20, 30] n1 = len(a1) if n1 > 2: print(f'{n1} is greater than two')

Yes, but your example is less efficient than the new code. Remember: every variable is an assignment into a dict, and every lookup a query into a dict. Reducing name lookups and assignments can yield good speedup in tight loops. For instance, caching os.path.join, os.path.split into local names can significantly speed up tight loops iterating over a filesystem. For example, os.path.split is potentially 5 dictionary l…

Local variables aren’t stored in a dict, likewise when a class has defined __slots__. Globals, modules and usual classes do use dicts internally, but locals do not. So from the efficiency standpoint it’s (almost?) the same. I haven’t checked the bytecode, maybe there is some slight difference.

Re: What’s New in Python 3.8

#157
post #92

Earlier quoted context omitted.

The issue, in my opinion, is when you want something like this: while m := f(many, args): # do stuff with m Now, if you're writing this in Python 3.7, you often end up with some code duplication: m = f(many, args) while m: # do stuff with m m = f(many, args) # duplicate Or something like this: while True: m = f(many, args) if not m: break # do stuff with m Personally, I consider the last version to be the most elegan…

Why not something like this: def f_iter(many, args): while True: m = f(many, args) if m: yield m else: raise StopIteration ... for m in f_iter(many, args): # do stuff with m This way you’re isolating all the initialization logic, error handling, etc. And you can focus on your domain logic in your client code.

so i should write a whole other function that iterates over a list instead of being happy that := exists?

Re: What’s New in Python 3.8

#158
post #127

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?

Though not a language feature per say, I would love if the language would take on the packaging ecosystem and deliver a ground up approach that isn't just a kludge on a kludge on a kludge.

While not official, doesn't conda solve this problem? (Except for not containing the latest versions).

Re: What’s New in Python 3.8

#160
The new assignment expression is great and I wish that an optional operator (`?`) and Elvis operator (`?:`) will make it to Python one day, too. I would love to write:

    v = obj?.prop1?.prop2 ?: "default"
instead of long if conditions:

    v = obj.prop1.prop2 if obj and obj.prop1 and obj.prop1.prop2 else "default"
A PEP for Python 3.8 existed but has been deferred: https://www.python.org/dev/peps/pep-0505/
Post reply on HN