Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Andrea Ciccotta Avatar asked Jan 30 '21 18:01

Andrea Ciccotta


People also ask

What is structural pattern matching in Python?

'Structural Pattern Matching' was newly introduced in Python 3.10. The syntax for this new feature was proposed in PEP 622 in JUne 2020. The pattern matching statement of Python was inspired by similar syntax found in Scala, Erlang, and other languages.

Can you pattern match in Python?

Introducing Python structural pattern matching The match/case statement follows the same basic outline as switch/case . It takes an object, tests the object against one or more match patterns, and takes an action if it finds a match. Each case statement is followed by a pattern to match against.

Does Python 3.9 have match?

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.

Is Python 3.8 Match case available?

NOTE: The match case structure is available for python version 3.10 and onwards.


Video Answer


3 Answers

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)
like image 196
Daniel Lenz Avatar answered Oct 19 '22 09:10

Daniel Lenz


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.

like image 5
Hultner Avatar answered Oct 19 '22 11:10

Hultner


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 .

like image 2
Travis G Avatar answered Oct 19 '22 09:10

Travis G