Live data from Hacker News

What’s New in Python 3.8

docs.python.org

241–250 of 381 posts

Re: What’s New in Python 3.8

#241
This got missed from the release announcement, but now there's `functools.singledispatchmethod`,[1] as the class method sibling to `functools.singledispatch`.[2] This allows you to overload the implementation of a function (and now a method) based on the type of its first argument. This saves you writing code like:

    def foo(bar):
        if isinstance(bar, Quux):
            # Treat bar as a Quux

        elif isinstance(bar, Xyzzy):
            # Treat bar as an Xyzzy

        # etc.
I understand runtime type checking like that is considered a bit of a Python antipattern. With `singledispatch`, you can do this instead:

    @singledispatch
    def foo(bar:Quux):
        # Quux implementation

    @foo.register
    def _(bar:Xyzzy):
        # Xyzzy implementation
With `singledispatchmethod`, you can now also do this to class methods, where the type of the first non-self/class is used by the interpreter to check the type, based on its annotation (or using the argument to its `register` method). You could mimic this behaviour using `singledispatch` in your constructor, but this syntax is much nicer.

[1] https://docs.python.org/3/library/functools.html#functools.s...

[2] https://docs.python.org/3/library/functools.html#functools.s...

Re: What’s New in Python 3.8

#242
post #83

Earlier quoted context omitted.

The f-strings from 3.6 are a (relatively) recent feature that I have absolutely loved. I'd go so far as to say they are my favorite feature introduced by Python 3. I'm also looking forward to PEP-554 [0], which allows for "subinterpreters" for running concurrent code without removing the GIL or incurring the overhead of subprocesses. [0] https://www.python.org/dev/peps/pep-0554/

f-strings are great. Much nicer than ".format". I hope in a next iteration of the language all strings will be f-strings by default, avoiding the need to prefix them by a silly "f".

[deleted]

Re: What’s New in Python 3.8

#243
post #39

Earlier quoted context omitted.

I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it: https://github.com/eveem-org/panoramix (source code for Eveem.org, which is arguably the best decompiler for Ethereum smart contracts out there. you can see a lot of pattern matching in pano/simplify.py , and I found no way to do it without extending the language/walrus while main…

Just a suggestion... I went to your repo, and it wasn't clear to me what Panoramix actually is or does. So I checked out Eveem.org, and even on the about page, it wasn't clear to me what Eveem was. But with that context, coming back to your GitHub page, I was able to get the gist of it. It might be helpful to have a section above the installation section that gives a little context / tells about the project. And in y…

Thank for the feedback! My first open-source project. I will improve this :)

Re: What’s New in Python 3.8

#244
post #39

Earlier quoted context omitted.

I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it: https://github.com/eveem-org/panoramix (source code for Eveem.org, which is arguably the best decompiler for Ethereum smart contracts out there. you can see a lot of pattern matching in pano/simplify.py , and I found no way to do it without extending the language/walrus while main…

After a quick read through, I personally find this syntax much harder to read using operator overloads than it would have been using a series of comparison functions. Tilde is a very obtuse operator and I don't know that this really buys you anything. Feels like a case of preferring cleverness over readability/usability/maintainability.

I actually had a series of comparison functions initially, but those weren't that readable.

To give you an example, let's look at this statement:

if exp ~ ('mask_shl', int:size, int:offset, -offset, :e): ...

It is the same as writing: if type(exp) == tuple and len(exp) == 5 and exp[0]=='mask_shl' and type(exp[1])==int aand type(exp[2])==int and exp[3] == -exp[1]: size, offset, e = exp[1], exp[2], exp[4] ...

If you can figure out syntax that makes this sort of matches more readable, I'll gladly use it :)

Re: What’s New in Python 3.8

#245
post #58
post #39

Earlier quoted context omitted.

I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it: https://github.com/eveem-org/panoramix (source code for Eveem.org, which is arguably the best decompiler for Ethereum smart contracts out there. you can see a lot of pattern matching in pano/simplify.py , and I found no way to do it without extending the language/walrus while main…

This is super interesting! I've never seen codecs used to introduce language features like this... Do you have any good references for getting started with using codecs? Also, I wonder if there's an easy way to implement this in a jupyter notebook, without fiddling with the kernel... (issues of whether it's a good idea aside, definitely seems useful to know about.. )

Thanks! The idea for using codecs is not mine - found it on StackOverflow somewhere. Googling for "how to add custom language statements python" should give you some results, this included :)

Also, in my case I had to use Codecs because a new operator causes AST-parser to not go through. But if you have some kind of syntax that will be compatible with Python AST, then you can implement it even easier.

You can google Python AST to get a lot of useful information :)

Re: What’s New in Python 3.8

#246
post #64
post #39

Earlier quoted context omitted.

I actually found an immense use for Walrus - used it to hack Python into doing pattern matching that is way more readable than without it: https://github.com/eveem-org/panoramix (source code for Eveem.org, which is arguably the best decompiler for Ethereum smart contracts out there. you can see a lot of pattern matching in pano/simplify.py , and I found no way to do it without extending the language/walrus while main…

You should link to a snippet that actually uses := rather than to the top level project.

Ah yes, sorry:

https://github.com/eveem-org/panoramix/blob/bc55be84a9a6abb1...

It uses "~" custom operator, but implementing it would not be possible without ":=" operator (see /tilde directory).

Re: What’s New in Python 3.8

#247
post #5

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.

I'm stoked about the walrus operator. Ever since I heard it was being added I've grumbled when writing code that would have been clearer with it. Of course I have to ask, what was your preferred syntax?

Not OP, but maybe it's "as":

  if re.match(pattern, string) as m:
      #use m
Seems a bit more Pythonic, as "as" is already used like this with "with".

Either one would be fine with me and useful.

Re: What’s New in Python 3.8

#248
post #175

Earlier quoted context omitted.

That's a good use case for a linter. Ban outdated constructs in your code, but still allow you to depend on things that use them. Beats another 2->3 split again.

Yeah, and I already use linters to remove all outdated constructs; but... It's a pity that "There's only one way to do it" doesn't work in 2019 :-(

It was never "there's only one way to do it".

It is: "There should be one-- and preferably only one --obvious way to do it."

The core of that statement is "There should be one obvious way to do it" - there's no "only" there, it just says that when you need to do something, there should be an/some obvious way to do it. Then, preferably, that should be the only obvious way — though of course that doesn't preclude there being many other less-obvious ways.

With string interpolation we've certainly now got multiple ways to do it; that doesn't violate the principle. I agree that at least two of those are "obvious" (f-strings and .format()), and you could argue that %-interpolation is obvious too — but none of that is in violation of the principle that there should be an obvious way to do it, just of the preference that there's only one.

Finally it's worth remembering where this idea came from: it was in contrast to the perl mantra of "there's more than one way to do it", and a reaction to the resultant confusion frequently experienced when reading someone else's perl code — this was python saying "we're not perl, we value clarity and comprehension". I think that much of that problem was bound up in perl's syntax choices, and that in the cases in python where there's more than one way to do it (say, string formatting, dataclasses/attrs/namedtuples/etc.), it's usually pretty obvious what machinery is actually being used. When was the last time you looked at a line of python and said "I have no idea what the fuck is going on here?" That was a frequent experience with perl in the heady days of the late 1990s.

Re: What’s New in Python 3.8

#249

This got missed from the release announcement, but now there's `functools.singledispatchmethod`,[1] as the class method sibling to `functools.singledispatch`.[2] This allows you to overload the implementation of a function (and now a method) based on the type of its first argument. This saves you writing code like: def foo(bar): if isinstance(bar, Quux): # Treat bar as a Quux elif isinstance(bar, Xyzzy): # Treat bar…

I can definitely imagine some places where this replaces type-checking, but it still seems like a bit of an unfortunate anti-pattern to me, since it's really a sort of C/C++ style function prototype match.

My immediate thought is that it's going to be hard for PyCharm to reliably point me to a function definition.

Re: What’s New in Python 3.8

#250

This got missed from the release announcement, but now there's `functools.singledispatchmethod`,[1] as the class method sibling to `functools.singledispatch`.[2] This allows you to overload the implementation of a function (and now a method) based on the type of its first argument. This saves you writing code like: def foo(bar): if isinstance(bar, Quux): # Treat bar as a Quux elif isinstance(bar, Xyzzy): # Treat bar…

Nice. I missed this feature only a few weeks ago. Good to know it's landed!
Post reply on HN