Where to put the error handing test?

Peng Yu pengyu.ut at gmail.com
Mon Nov 23 22:15:48 EST 2009


Suppose that I have function f() that calls g(), I can put a test on
the argument 'x' in either g() or f(). I'm wondering what is the
common practice.

My thought is that if I put the test in g(x), the code of g(x) is
safer, but the test is not necessary when g() is called by h().

If I put the test in f(), then g() becomes more efficient when other
code call g() and guarantee x will pass the test even though the test
code in not in g(). But there might be some caller of g() that pass an
'x' that might not pass the test, if there were the test in g().

def g(x):
   # I can put the code here to test whether x satisfies certain conditions.
   blah, blah

def f(x):
   blah, blah
   #do something on x
   # I can also put the code here to test whether x satisfies certain
conditions.
   g(x)


def h()
   blah
   g(x)#x here are guaranteed to pass the test



More information about the Python-list mailing list