Python Structural Pattern Matching
Asked Answered
M

4

8

I'm not able to run this code:

match shape:
    case Point(x, y):
        ...
    case Rectangle(x, y, _, _):
        ...
print(x, y)

I can't find the match keyword in Python.

I found it here: https://www.python.org/dev/peps/pep-0622/#the-match-statement

Any idea?

Menstruation answered 30/1, 2021 at 18:58 Comment(2)
"Status: Superseded" says the link, it also gives a reference implementation.Protean
But..... what is the question here?Improvise
B
15

Update 2021-04-19: Python 3.10 will introduce a structural pattern matching. See the other excellent answers for more details on that.

The source you're referring to is a PEP (Python Enhancement Proposal), it has not been implemented in a stable release yet. Furthermore, the PEP has been superseded by PEP634.

As of early 2021, the match keyword does not exist in the released Python versions <= 3.9.

Since Python doesn't have any functionality similar to switch/case in other languages, you'd typically use nested if/elif/else statements or a dictionary.

Here's an example based on your questions, even though it's not immediately clear to me what you're trying to achieve.


class Point:
   def __init__(self, x, y):
       pass

class Rectangle:
   def __init__(self, x1, y1, x2=0, y2=0):
       pass

shapes = dict(
    point=Point,
    rectangle=Rectangle,
)

my_obj = shapes['point'](x, y)
Betancourt answered 30/1, 2021 at 19:8 Comment(6)
I lost 20 minutes searching python.org site this famous match-case, announced as implemented, and got a lot of long philosophical views and even tutorials on it, until I finally reached your answer and discovered it was never implemented. Thanks a lot.Polad
@Polad I'm surprised by your comment, in particular when written end April 2021: "it was never implemented" is clearly wrong, it was just not released yet in the stable Python (at the time of your comment). Since May 3rd, 2021, you may download and try the pattern matching in the beta version of Python3.10.Vogel
@Joël: If you think the initial answer was wrong when mentioning "it has not been implemented yet. Furthermore, the PEP has been superseded by PEP634", you should direct you comment to the poster, not me. For the rest, you may consider a beta version an official release of the product good for production, but I do not and won't use it to solve one problem while creating 10 new ones.Polad
@Joël Thanks for your feedback! I agree that the wording wasn't on point and made some adjustments.Betancourt
@Polad you missed my point: the poster answer identified what was not implemented. In your comment, your sentence read "this famous match-case (...) was never implemented.", and this is what I commented. The rest of my comment is not to say one wants to use a beta version, but rather just indicating that implementation is well under way ;-)Vogel
@Joël: Now I understand your point, note my comment was written two weeks prior to the beta release.Polad
H
5

As of March 2021 structural pattern matching is not only officially accepted but also available in the latest alpha and development version of Python 3.10. I wrote an article "Get started with Pattern Matching in Python, today!" last week detailing how this could be achieved but I'll do a short recap here.

Install 3.10-dev/a6 via pyenv

First make sure you've got pyenv installed and set up. At this point simply do

pyenv install 3.10-dev

You can now activate the beta locally and run the interpreter.

pyenv local 3.10-dev
python
Python 3.10.0a6+ (heads/master:87f649a409, Mar 11 2021, 16:29:20) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Run via docker container

You can also use docker if you don't care about having 3.10 running directly in your local system. The new alpha 6 is already up and a 3.10.0a6 interpreter can easily be launched in a container like this.

docker run -it python:3.10.0a6-buster

There you have it, two different ways to use/test the new structural pattern matching in python.

Note: This is still an early release, the completed version will be available in October, so don't build your production stack on this feature just yet. But if you want to experiment with future concepts, you can do so today.

Hubbs answered 15/3, 2021 at 9:4 Comment(0)
D
2

PEP 634,PEP 635 and PEP 636 are three of the pending Python Enhancement Proposals that are yet to be accepted and then implemented.

That means it is just a proof of concept that the requestors would like to see in coming future and it is not yet has been developed. Also there is no surety it would ever be PEP tends to be more of a wishlist .

Deuteronomy answered 30/1, 2021 at 19:17 Comment(0)
M
0

As others said already.

Structural pattern matching is not implemented so far. It was just a PEP. It was PEP 622 originally and became PEP 634, PEP 635 and PEP 636

However: As of 8th of February 2021 structural pattern matching PEP 634 and its companion PEPs PEP 635 and PEP 636 have been accepted by the Python Steering Council.

Structural pattern matching was provoking quite some controversies, but it seems, that in the end it was chosen, as similar constructs exist in many modern languaged like in Haskell, Erlang and Scala to Elixir and Ruby. (A proposal for JavaScript is also under consideration.)

Refer for example to article on lwm.net

The Python steering council has, after some discussion, accepted the controversial proposal to add a pattern-matching primitive to the language. "We acknowledge that Pattern Matching is an extensive change to Python and that reaching consensus across the entire community is close to impossible. Different people have reservations or concerns around different aspects of the semantics and the syntax (as does the Steering Council). In spite of this, after much deliberation, reviewing all conversations around these PEPs, as well as competing proposals and existing poll results, and after several in-person discussions with the PEP authors, we are confident that Pattern Matching as specified in PEP 634, et al, will be a great addition to the Python language."

Minimus answered 18/2, 2021 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.