Exception as the primary error handling mechanism?
phlip2005 at gmail.com
Wed Jan 6 00:51:29 CET 2010
Peng Yu wrote:
> Otherwise, could some python expert explain to me why exception is
> widely used for error handling in python? Is it because the efficiency
> is not the primary goal of python?
It's not about efficiency, it's about making assumptions for the
programmer about what kind of rigor they need.
Why can't int('nonnumeric') return None?
Why can't a Django Record.objects.get(pk=-1) return a None? That's
what it's for.
(A related question - why can't I just go 'if record = method(): use
(record)'. Why extra lines just to trap and assign the variable before
There are workarounds that sometimes benefit the code. In the case of
collections, like recordsets, you might be better off using for ... all
Then your controlled block efficiently does not happen if it saw no
records. "Efficiently" in terms of programmer complexity - the number
and meaning of lines that a programmer must comprehend.
And why can't Record.objects.get(pk='nonnumeric') return None?
Because, of course, deep inside it calls int(). I can't simplify the
calling code, and rely on garbage-in-None-out, because Python decided
which simplifications I should avoid with self-righteous indignation.
The Samurai Principle (return victorious, or not at all) is very
useful, sometimes. But other times it just prematurely depletes your
supply of Samurai...
More information about the Python-list