|
|
Subscribe / Log in / New account

Python structural pattern matching morphs again

Python structural pattern matching morphs again

Posted Dec 3, 2020 9:44 UTC (Thu) by lxsameer (guest, #65438)
In reply to: Python structural pattern matching morphs again by logang
Parent article: Python structural pattern matching morphs again

Totally agree, The rational behind it doesn't make sense either, In order to utilize pattern matching a language has to have a better type system than what Python has at the moment and beside that they're trying to address some issues that Protocols/Traits are better solutions.

To me, it seems that because pattern matching and type systems are a trend in past few years they want them in Python otherwise it's violates Python's Zen and would be mess.


to post comments

Python structural pattern matching morphs again

Posted Dec 3, 2020 10:41 UTC (Thu) by rsidd (subscriber, #2582) [Link] (1 responses)

Yes, a recent trend if by "last few years" you mean "since the 1970s" (ML). Python has lifted several things from functional languages. This is another and is extremely useful. Those languages largely use flexible (polymorphic / inferred) types too.

Python structural pattern matching morphs again

Posted Dec 3, 2020 17:32 UTC (Thu) by nybble41 (subscriber, #55106) [Link]

> Those languages largely use flexible (polymorphic / inferred) types too.

Inferred static types are very different from dynamic types, even if they appear superficially similar at the source level. Even with polymorphism, the type of the value being matched must be known in advance for each particular instance. The compiler can use this information to generate jump tables rather than long if/else chains and to statically test for exhaustiveness in the match patterns. The simplest pattern matches (e.g. destructuring a product type) can be optimized out altogether.

You can get approximately the same behavior with runtime tests in dynamic languages, but the performance cost is considerable.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds