[Python-ideas] Should decimal.InvalidOperation subclass ValueError?
Stephen J. Turnbull
stephen at xemacs.org
Wed May 25 05:23:29 EDT 2016
Guido van Rossum writes:
> Well, ZeroDivisionError doesn't derive from ValueError, does it? So
> it may not be a good idea, but it's certainly a tradition.
I think it's deeper than merely tradition (though that doesn't make it
a good idea, either). One way to look at these exceptions is that you
shouldn't apply that operation to that value (ArithmeticError), and
another is that you shouldn't pass that value to that operation
(ValueError). A distinction without a difference, I suppose, after
thinking about how I would apply it to Python. Eg (and most
tellingly, I think) as Python implements division, first you ask the
numerator if it knows how to divide itself by something. It says
"sure, here's my method," the method asks "what number?", and *then*
when you tell it "zero", it says, "sorry, I don't do zero!" and that's
a ValueError.
The example above is sort of an artifact of division being a binary
operation, so even with an OO implementation you end up passing a
value to function that then decides it doesn't like it. In a language
like Haskell, where a function always takes one argument, and returns
a new function if the program wants to handle more than one argument,
the distinction might make more sense. But I don't see a reason to
pursue that line of thought here, so I won't even try.
More information about the Python-ideas
mailing list