4 ms·
Doesn't Python 3.10 add pattern matching? [1]. Seems to make this much more palatable. Specifically look at [2]. [1] https://peps.python.org/pep-0636/ https://
by angarg12 4y ago
Doesn't Python 3.10 add pattern matching? [1]. Seems to make this much more palatable. Specifically look at [2].
[1] https://peps.python.org/pep-0636/ https://peps.python.org/pep-0636/
[2] https://peps.python.org/pep-0636/#adding-a-ui-matching-objects https://peps.python.org/pep-0636/#adding-a-ui-matching-objec...
- dahfizz 4y agoYes it does! It's such a nice addition to the language. It does make error objects more palatable, but I still don't see the benefit of error objects over exceptions. You still lose stack traces, and a universal way to get error messages. You're still taking a risk that the caller will miss a case and not handle the error at all, thereby shrinking the "pit of success". Also, FWIW, the code in question is 5 years old. It seems to be more of an idealogical question than a pragmatic one.
- tialaramex 4y agoExceptions come with program flow. Didn't need/ want that flow? Too bad, you get it anyway and must re-structure your program accordingly. Suppose there are two consumers of my function F, which usually returns a Digit but sometimes instead has a problem. Should F throw ProblemException or should its return type be a Sum Type with both Digit and Problem ? Alice uses F in some hot code that doesn't immediately care about the problem or the digits, while Bob is only using F in an edge case and needs the result immediately. For Alice, needing to try F and handle the exceptions (which Alice doesn't ever care about immediately) is annoying. For Bob it works fine. But, Bob can also write code to handle the Problem next to his code for handling the Digit, we were forcing Alice to handle ProblemException, but Bob can choose where to do something with Problem as it suits him.
- mafro 4y agoAlice isn't forced into using anything. You appear to be forgetting that raising exceptions as part of program flow is standard and pythonic. It's OK to disagree with that, but as it's part of the language and part of the python modus operandi let's not go out of our way to illustrate our dislike.. Just use another language
- tialaramex 4y ago> Alice isn't forced into using anything. How so? Do you just mean, "Well, Alice needn't use Python" ?
- dahfizz 4y agoAlice can use `with supress` to ignore the exception. That's just as much, if not less, extra control flow as forcing the caller to check the returned object's type.
- tialaramex 4y agoSuppression destroys the problem, but Alice does want the problem, just not in the middle of this say, tight loop. With a sum type Alice can put problems, together with Digits, in a pile to be handled later.
- dahfizz 4y agoThen Alice can catch the exceptions and put them into a list for later, if that's what she really wants. This feels like a pretty contrived example. The general case is that `get_foo()` should return a Foo, or throw an exception if it can't. It shouldn't return a Foo, or a BarError, or a BazError. The fact that you can tuck these completely different objects away for processing later without knowing what they are is not a desirable feature. That's just going to be a source of bugs. In a language with a stronger type system, I see the benefit of option types and the like. But in Python, having a list of `my_foos` contain mostly Foos, but also some garbage, is not something you want.
- tialaramex 4y ago> This feels like a pretty contrived example. Well, it's certainly not popular in Python today. And it seems the reaction from most Python programmers is they'd just force Alice to take the hit, whereupon presumably Alice ceases using Python for this application, and that's maybe fine, it's not as though there aren't other languages. But it seems like a shame because Python can do this the other way. > In a language with a stronger type system, I see the benefit My instinct is that the direction of Python is towards a stronger type system. I remember when Python's type hints were a very obscure feature few ever cared about and now it seems like every other Python story involves the type system and type annotation is strongly encouraged. But I could be wrong.