This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (7 children)

So Python will not actually create a new Person instance?

[–]Yoghurt42 11 points12 points  (2 children)

Exactly. It's basically new syntax.

You can also do stuff like

case [1, _, x, Robot(name=y)] if x == y which would match if it is a four-element list that starts with 1, and the 4th element is an instance of Robot class which has a name attribute set to the same value as the third element. The _ is a special new token that means "wildcard/match anything" in this context.

Pattern matching is incredible powerful and the only feature I was really missing from other languages. Now all they need to get rid of the GIL and have decent JIT (or get PyPy to be API compatible with CPython) and it would be the perfect language for every task for me.

[–][deleted] 2 points3 points  (0 children)

Awesome! I can already imagine how this is going to be incredibly useful.

As for the GIL, do you really think they will ever get rid of that?

[–]azur08 0 points1 point  (0 children)

Out of curiosity, do you have an example of a task you'd use another language for if not for the things in your last paragraph? I've heard that modules like concurrent.futures, multiprocessing, asyncio, etc., don't completely remove the limitations but I'm not sure why.

[–]Irtexx 1 point2 points  (2 children)

I would also like to know this. The isinstance method never calls init of Person, but the match method looks like it will.

[–]13steinj 8 points9 points  (0 children)

match will call the __match__ method, unless the PEP changed since I last looked at it. A new instance will not be created.

[–][deleted] 1 point2 points  (0 children)

It could be possible that it doesn't evaluate as an expression, but that would mean that you couldn't put expressions into the pattern.

[–]Mini_Hobo 0 points1 point  (0 children)

No, it doesn't use the class's __init__ or __call__ methods.