Is there a good writeup / talk about the implementation of PEP 634?

In particular, I’m interested in learning about pattern matching of classes. Seems like black magic to me.

Of course, the implementation is there but I cannot wrap my head around it without learning some core concepts.

1 Like

PEP 634 itself serves as the specification. @brandtbucher gave a talk about this at PyCon US 2022 that might be helpful: Talk - Brandt Bucher: A Perfect Match The history, design, implementation, and future of Python's... - YouTube

1 Like

Interesting video, doesn’t cover the interesting examples though…

One example at 16:09 it does cover seems broken:

PATTERN = r"(.*) is (closed|still under investigation)".
match = re.match(PATTERN, "The Case of the Missing Spam is still under investigation.")

match match:
    case case, "closed":
        print (f"Wow, they finally solved (case}!")
    case case, "still under investigation":
        print (f"I wonder when they will solve (case)!")
    case None:
        print ( "Why aren't they looking into this?")

match won’t unpack unless match.groups() is used, but the latter will raise when there is no match (match is None).

I wonder if it could be rewritten for class matching.

Raymond gave a talk with lots of examples at PyCon Italia 2022: Structural Pattern Matching in the Real World - Raymond Hettinger - YouTube

1 Like

Interesting examples there! Makes me wish for someone to put together Structural Pattern Howto for

As a start, it would seem to me to be a good idea to port the content of PEP 636 - Structural Pattern Matching: Tutorial over to be an actual tutorial in the Python docs. It’s fairly complete and well-written as is, so it would likely just take some mechanical adjustments, I would think.


There’s a tutorial that is based of PEP 636 in the What’s New of 3.10 What’s New In Python 3.10 — Python 3.13.0a0 documentation