Andrea Ciccotta
Andrea Ciccotta

Reputation: 672

Python Structural Pattern Matching

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?

Upvotes: 8

Views: 6406

Answers (4)

Travis G
Travis G

Reputation: 1602

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, as PEP tends to be more of a wishlist .

Upvotes: 2

Daniel Lenz
Daniel Lenz

Reputation: 3857

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)

Upvotes: 15

Hultner
Hultner

Reputation: 3780

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.

Upvotes: 5

gelonida
gelonida

Reputation: 5630

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."

Upvotes: 0

Related Questions