verify the return value of a function

Terry Reedy tjreedy at udel.edu
Fri Jan 20 15:42:45 EST 2012


On 1/20/2012 10:07 AM, Steven D'Aprano wrote:

> What should be avoided, when possible, is over-reliance on isinstance
> checks instead of protocol or interface checks. For example, don't check
> for a list if your function doesn't *need* a list but would be happy with
> a tuple or some other sequence.

In other words, do not use isinstance to artificially limit the input 
domain of a function. The generic or polymorphic nature of (builtin) 
operators and functions is a major feature of Python.

On the other hand, the output range of a function is typically much more 
limited as to type. Complete testing requires testing the specified 
output type. For instance, sorted(iterable) is documented as producing a 
sorted list, so 'type(output) is list' is an appropriate test.

-- 
Terry Jan Reedy




More information about the Python-list mailing list