3 ms·
I would be more careful about comparing "match" and "switch". If people assume that the match operator is a switch operator, they will not be able to explain th
by probably_wrong 6y ago
I would be more careful about comparing "match" and "switch". If people assume that the match operator is a switch operator, they will not be able to explain the difference between these two behaviors: https://news.ycombinator.com/item?id=26082225 https://news.ycombinator.com/item?id=26082225.
I get it that you can use "match" as if it were a "switch", but I think it's important to drill in every Python developer's head that they are not the same thing.
- exyi 6y agoThis is the bad part of Python's match, I don't understand why they want to do it this way - in neither Scala, Rust, F#, C# you'd would (AFAIK) hit such issue. For example C# example of both behaviors: https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpHoDGA9gHYDOALugJYV0Vk0BiZArhcOgLzoALAAZBAbkLE0mOMPQBZABSM65YAFMAlPknFiVAO70aJABbpFarTqR67xEgEMq69C3ZceIXffs1TAE5kBm7qIQCiAB4k6gAONPSUigBEAHKs6BzcwMmaEra+AL4+eiXEAPTl6ABuVOjqkbEANvQkxmVEHeiGxmYWVtoEBb4Ozq7VjgFurFleXb7+QSEUYehRMfGJFCnpdLO8/o50ALZHZup1jk1NufkjxcPoD4VAA== https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpHoDG...
- duckerude 6y agoI don't know about those other languages, but Rust needs static type information to solve it (which Python doesn't have). Take this code: static NOT_FOUND: i32 = 404; match 403 { NOT_FOUND => println!("Not found"), _ => println!("Other"), } It doesn't compile: error[E0530]: match bindings cannot shadow statics It does compile if you change static to const (and then it does what you want). But if you misspell NOT_FOUND so it doesn't shadow a static then it misbehaves in the same way as the Python version, with only a few warnings to show for it. Python has the limitation that there's no fundamental difference between ordinary variables and constants. I don't know of an alternative proposal for Python that isn't burdensome or also dangerously surprising.
- pansa2 6y agoThat C# example is so much nicer than Python’s approach of casually breaking fundamental programming principles [0]. I wonder why Python’s designers didn’t use something similar? [0] https://twitter.com/brandon_rhodes/status/1360226108399099909 https://twitter.com/brandon_rhodes/status/136022610839909990...
- smitty1e 6y agoPython didn't handle enumerations, either, until around 2.6 (?) when they were added as a module. The match operator (I guess the new keyword means it can't be a module) seems to be a similarly well-considered additon to the enum. Python appears to add common features after more front-end work than the competition. That said, one fears the higher release tempo could lead to "featuritis". I still have yet to wield the walrus operator that was recently added in any code.