The Samurai Principle

Bruno Desthuilliers bdesth.quelquechose at
Tue Sep 7 22:15:42 CEST 2010

Phlip a écrit :
> On Sep 7, 10:36 am, Ian Kelly < at> wrote:
>> On Tue, Sep 7, 2010 at 10:02 AM, Phlip <phlip2... at> wrote:
>>> Back to the topic, I tend to do this:
>>>  for record in Model.objects.filter(pk=42):
>>>     return record
>>>  return sentinel
>> How is that any better than just catching the exception?
>> try:
>>     return Model.objects.get(pk=42)
>> except Model.DoesNotExist:
>>     return sentinel
>> The flow of control is much clearer this way.
> It reminds me of Visual Basic.

Strange enough, your own code snippet reminds me of what I used to find
when fixing VB programs some ten years ago.

> And no it's not "much clearer".

It is for any Python programmer - it's even TheOneObviousWay.

> Exceptions are for catastrophic errors

Chapter and verse, please ?

Exceptions are for exceptional situations. When you call queryset.get,
you do expect to have one single instance matching the lookup - specialy
when doing a pk lookup.

> AAAND you need to test that the DoesNotExist occurs for the exact
> reason you expect.

Bullshit. The only reason you'd get this exception is because there's no
record matching your where clause.

> Your except is not complete.

Why so ?

> Making it complete is
> very hard, and will break as soon as the model changes.

Why so ?

More information about the Python-list mailing list