6 ms·
Yeah, I think the author focused a bit too much on structural pattern matching being a replacement/alternative for if...elif blocks. It will be much more usefu
by jimminy 5y ago
Yeah, I think the author focused a bit too much on structural pattern matching being a replacement/alternative for if...elif blocks.
It will be much more useful as an alternative to dispatch dictionaries, which are more a side-effect of the language lacking any case based control-flow.
- ufo 5y agoBy the way, does anyone here know how Python compiles the match statements under the hood? Are they converted into something equivalent to if-elif, which tries each option in sequence? Or does it do something more clever?
- mixmastamyk 5y agoThe author replied here that yes, the bytecode is simple, with the exception of the Class matching. https://news.ycombinator.com/threads?id=benhoyt https://news.ycombinator.com/threads?id=benhoyt
- kristjansson 5y agoYeah, there are several giant dictionaries (variously class to function, string to class, etc.) in just the project I’ve worked on most recently that fit that description. Though considered from that perspective, it’s actually a bit of a disadvantage that the new match statement is a part of the language. It’s nice to dynamically extend (either as a first party developer or third party user) that sort of dictionary, effectively adding new branches to the implicit switch statement. A match statement is going to be fixed as written. Probably a readability and consistency win to remove spooky-action-at-a-distance, but at the cost of a bit of (ugly, pragmatic) usability