Clarity vs. code reuse/generality
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Jul 4 16:01:00 EDT 2009
Paul Rubin wrote:
> Invalid input data is not considered impossible and doesn't imply a
> broken program, so assert statements are not the appropriate way to
> check for it. I like to use a function like
>
> def check(condition, msg="data error"):
> if not condition: raise ValueError, msg
>
> ...
> check (x >= 0, "invalid x") # raises ValueError if x is negative
> y = sqrt(x)
And I curse such uses, since I don't get to see the troublesome value,
or why it is troublesome. In the above case, y = sqrt(x) at least
raises ValueError('math domain error'), which is more information than
you are providing.
How about:
...
if x >= 0: raise ValueError('x = %r not allowed (negative)?' % x)
...
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list