[Tutor] evaluating AND

John Fouhy john at fouhy.net
Fri Sep 14 06:47:17 CEST 2007


On 14/09/2007, Rikard Bosnjakovic <rikard.bosnjakovic at gmail.com> wrote:
> On 14/09/2007, Terry Carroll <carroll at tjc.com> wrote:
> > The second one, which just checks "if x" and is satisfied with any false
> > value, including an empty tuple, does not raise the error condition, even
> > though the data is bad.  This is a bad thing.
> My point is that if you think it's bad for a function to receive
> incorrect data you should do exhaustive checks for the input to make
> sure it is of correct type, size and whatever the requirements might
> be, not just check if it's a tuple or not.

I think Terry's viewpoint is that a function should raise an exception
if it receives bad data.  You don't need to do exhaustive
isinstance()/type() tests; just use the data as if it is what you're
expecting and let errors happen if the data is wrong.

What's important is that the function should not return valid output
when given invalid input.  That's what could happen with Terry's
hypothetical test02() if you give it an empty list.

OTOH, if you give either function an integer, both will raise
exceptions, which is fine.

You could change the function like this, though, to ensure the list
length is correct:

>>> def test01(x):
...  if x is not None:
...   assert len(x)==2
...   if x[0]>0:
...    return x[1]/x[0]
...

-- 
John.


More information about the Tutor mailing list