Comparison with False - something I don't understand

Stephen Hansen me+list/python at ixokai.io
Thu Dec 2 10:35:18 EST 2010


On 12/2/10 6:56 AM, Harishankar wrote:
> On Thu, 02 Dec 2010 08:44:11 -0600, Tim Chase wrote:
> 
>> On 12/02/2010 08:18 AM, Harishankar wrote:
>>> Here I'm using it to compare the result of a function where I
>>> specifically return False on error condition,
>>
>> This sounds exactly like the reason to use exceptions...you have an
>> exceptional error condition.
>>
>> -tkc
> 
> There are some reasons why I hate exceptions but that is a different 
> topic. However, in short I can say that personally:

To each his/her own, of course; but --

> 3. Philosophically I think exception handling is the wrong approach to 
> error management. 

Exceptions aren't about "error management"; they are about exceptional
conditions: some are errors, others are entirely normal situations you
know are going to happen (such as reaching the end of a sequence as you
iterate over it: that's not an error, but it is special). To be
philosophically opposed to them seems to me to be philosophically in
favor of race conditions.

> I have never grown up programming with exceptions in C 
> and I couldn't pick up the habit with python either. Did I mention that I 
> detest try blocks? try blocks seem ugly and destroy code clarity at least 
> in my view. And enclosing single statements under separate try blocks 
> seem to add a lot of clutter. 

? How do they destroy clarity or add clutter, since presumably you have
to deal with that "False" in some way with logical structure -- which
always does whitespace in Python. If not immediately, then up the call
stack (which seems to imply you should just not use a try/except and let
the exception unwind the stack to wherever in your code someone wants to
deal with it).

if is_correct():
    result = do_thing()
else:
    do_error_handling()

try:
    result = do_thing()
except KeyError:
    do_error_handling()

And as an aside, the more statements one puts into a try/except block:
and the more complicated a statement at that-- the more likely it is
they are going to mess up and do something Wrong.

Now, all that said: sure, in some situations I do prefer the "check
first" style of programming where exceptions don't end up being used.
Its not *wrong* to return False on an "error condition": its going
against the grain, though, and makes your code harder to deal with
long-term if only because now there's two separate mechanisms that
"errors" happen in it.

You can't totally do away with exceptions in Python, even if you try
very hard. So with that in mind, IMHO, the best approach is to just...
get over it, and learn to appreciate them :)

But, to each his or her own. :)

--

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20101202/100a24c9/attachment.sig>


More information about the Python-list mailing list