[Tutor] validation
Terry Carroll
carroll at tjc.com
Wed Aug 29 03:34:39 CEST 2007
On Tue, 28 Aug 2007, Kent Johnson wrote:
> Terry Carroll wrote:
>
> > 1A) a variation of option 1 (which is why I said "depending on how you
> > count" above): duck-typing with error recovery.
> >
> > def incr(n):
> > "returns an incremented value of n, or None if n is not incrementable"
> > try:
> > return n + 1
> > except:
> > return None
> >
> > This will try to return a value equal to N + 1, but if n does not quack
> > like a duck, it just returns None (and it's up to the caller to deal with
> > it).
>
> This is generally a bad idea. The problem is that you depend on the
> caller checking the return value for None.
Agreed. I was just trying to come up with a short example of catching the
exception and doing something different because of it. Admittedly,
returning None is not much of an "error recovery."
More information about the Tutor
mailing list