Error checking in Python
a.schmolck at gmx.net
Mon Jun 9 16:21:35 CEST 2003
BGM <no.spam at comcast.net> writes:
> Hello all:
> I am new to python, coming from a modest C background. Recently, I was
> writing some custom module in Python and I was wondering about one thing:
> Since the user of the module can pass any argument they wish, then it
> seemed like a good idea to check ,say, for type errors within the
> functions in the module.
Well it might seem like a good idea, but it isn't. In fact (unless in special
cases) it is a really bad idea. Since data in python, unlike in C, is typed,
type problems are reliably detected at runtime. So if someone passes an
argument of the wrong type into your function, you can in most cases be fairly
sure that this will produce a adequate error message.
Checking the type explicitly yourself typically offers no advantages: it will
only slow down your program, make it more verbose and less flexible: e.g. one
of the big advantages of dynamic typing is that often your code turns out to
work for types that you hadn't anticipated, without any rewriting.
So in summary, you'd only get the worst of both worlds (dynamic and static
typing), because you deny yourself the flexibility and conciseness that
dynamic typing offers without reaping the (minor) benefit of extra compile
time checks that static typing would give you.
More information about the Python-list