Error checking in Python

John J. Lee jjl at pobox.com
Mon Jun 9 19:38:04 EDT 2003


DavidW Blaschke <dwblas at yahoo.com> writes:

> With all due respect to the others who have posted a
> reply,  a very small slowdown is better than having a
> program abort.

Sigh.  That's not the point.

This has been discussed *so* many times before, with universal
acceptance amongst people who have actually listened to the arguments
and thought about it.

Google!

http://www.google.com/groups?as_q=LBYL%20group%3Acomp.lang.python
http://www.google.com/groups?as_q=EAFP%20group%3Acomp.lang.python


> BTW type checking can be done with:
>      if type(1) == type( var ) :
>         type("A") == 
>         etc.

When you *do* typecheck, use isinstance(a, b), not type(a) = b.


[...]
> Always check for "None" or an empty string first, and
> you have to return some sort of default error
> indicator if the variable is empty or can not be
> processed.

Is this a troll?  Have you heard of exceptions?


> In general, you should be able to call a function
> without worrying about any of the consequences.  It is
> the functions job to verify and alter when, and if
> possible.  An old, old saying is to write code that is
> iron-clad, meaning that it can handle almost anything
> that is thrown at it.  Sissy functions cost much more
> in the long run.  Good question though.  It shows that
> you want to develop good programming habits.

Well... the question of whether / when to assert invariants rather
than raise exceptions is an interesting one, and I certainly haven't
made up my mind.  The usual Python approach seems completely at odds
with Meyer's way of doing things (as in Eiffel), so I've been
surprised to see people doing that style of programming in Python.


John




More information about the Python-list mailing list