Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19165

Re: verify the return value of a function

From Terry Reedy <tjreedy@udel.edu>
Subject Re: verify the return value of a function
Date 2012-01-20 15:42 -0500
References <CAOuJsM=DonsS_Th=XEx72z_5HjmdvdE4HOrJzmB4NuBt3fVSSw@mail.gmail.com> <mailman.4876.1327054447.27778.python-list@python.org> <jfbrk7$vq8$1@speranza.aioe.org> <4f198330$0$29987$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.4894.1327092190.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: verify the return value of a function Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-01-20 11:13 +0100
  Re: verify the return value of a function Mel Wilson <mwilson@the-wire.com> - 2012-01-20 08:53 -0500
    Re: verify the return value of a function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-20 15:07 +0000
      Re: verify the return value of a function Terry Reedy <tjreedy@udel.edu> - 2012-01-20 15:42 -0500

csiph-web