An Object's Type
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Dec 6 13:24:49 EST 2007
Hrvoje Niksic a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at wtf.websiteburo.oops.com>
> writes:
>
>
>>So ask yourself: in which way will the final result be different
>>from would very probably happens without the "typecheking code" ? In
>>*both* cases, you end up with a runtime exception.
>
> The idea behind such type checks is to make sure type errors are
> caught as early as possible. Exceptions caught later, the philosophy
> goes, are harder to debug because by then the cause of the problem can
> be obscured. Consider an object of the wrong type passed to a method:
> the method can store the object in its own instance attribute, and
> keep working on something else. Then, much later, the faulty object
> gets actually used and the error is raised. By the time the exception
> is thrown, you have no idea where the offending object came from.
That's the theory, yes. In practice, when such a situation occurs, it's
usually easy to track down the problem: just add a temporary check in
the method (or around it - using a decorator), rerun the program and
you'll be done. Once the faulty code is corrected, remove the check.
More information about the Python-list
mailing list