The Samurai Principle

Phlip phlip2005 at gmail.com
Tue Sep 7 14:53:37 EDT 2010


On Sep 7, 11:36 am, Tim Chase <python.l... at tim.thechases.com> wrote:

> > And no it's not "much clearer". Exceptions are for catastrophic errors
> > that the caller should care not to handle. A "record not found" is not
> > a catastrophe.
>
> Exceptions are not limited to catastrophic errors, simply
> exceptional (not the common) cases.  E.g. iterators raising
> StopException when exhausted.

Exceptions are not "because we should only return one type of thing".
They are for situations which the caller should care not to handle.
Exceptions are for propagating. A "record not found" is an exemplary
example of a situation the caller _should_ handle.

>    items = list(MyModel.objects.filter(...))
>    if len(items) == 1:
>      do_something(items[0])
>    else:
>      what_the(...)

Both your version and mine read an entire cursor. But mine only rezzed
the first object, whereas yours rezzed every object in the cursor,
just to throw most of them away!



More information about the Python-list mailing list