Comparison with False - something I don't understand
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Dec 5 00:42:18 EST 2010
On Sun, 05 Dec 2010 04:13:02 +0000, Tim Harig wrote:
> Anything it is an obvious
> error *should* throw an exception.
Well, maybe... there are good use-cases for returning a sentinel. E.g.
str.find, or the use of quiet NANs in IEEE floating point and decimal
maths.
NANs and INFs in floating point maths are a good example of the right way
to do it. If you forget to check for a NAN, it will propagate through
your calculation. INF will, under some circumstances where it is
mathematically valid to do so, will disappear leaving a normal result.
This means you only need to check your result at the very end of the
calculation, not after every step.
str.find is more troublesome, because the sentinel -1 doesn't propagate
and is a common source of errors:
result = string[string.find(delim):]
will return a plausible-looking but incorrect result if delim is missing
from string. But the convenience and familiarity of str.find means it
will probably be around forever.
--
Steven
More information about the Python-list
mailing list