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 docs.python.org