3 ms·
Ah, I completely agree with what you have written here. My earlier post applied to using the if clause for matching to the variable value instead of capturing t
by pkolaczk 6y ago
Ah, I completely agree with what you have written here. My earlier post applied to using the if clause for matching to the variable value instead of capturing the value from the object.
I view pattern matching from a bit different angle - instead of viewing the case expression as answering "does the object match the pattern", I view it instead "can the object be deconstructed to a thing looking like the pattern".
That is, if you write the exactly same expression after matching on the right side, it should evaluate to the original object being matched. And I believe this holds in Python just as in other languages with pattern matching.
However, Python way of making it work is indeed a bit strange - because pattern matching is allowed to mutate variables from the outer scopes, while in some other languages I'm used to, capturing symbols just shadow the symbols in the outer scope, without changing them. This is a bit scary part of Python implementation.