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?
'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.
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.
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.
NOTE: The match case structure is available for python version 3.10 and onwards.
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)
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.
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.
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.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With