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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'instance,': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tuple': 0.09; 'output': 0.10; 'am,': 0.12; 'protocol': 0.15; 'artificially': 0.16; 'reedy': 0.16; 'sequence.': 0.16; 'subject:function': 0.16; 'wrote:': 0.16; 'jan': 0.19; 'input': 0.21; 'appropriate': 0.22; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'interface': 0.23; 'documented': 0.23; 'generic': 0.24; 'testing': 0.26; 'function': 0.27; 'producing': 0.28; "skip:' 10": 0.29; 'specified': 0.30; 'words,': 0.32; 'typically': 0.32; 'list': 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'to:addr:python-list': 0.33; 'function.': 0.34; 'test.': 0.34; 'header:X-Complaints-To:1': 0.34; 'example,': 0.36; 'checks': 0.36; 'list,': 0.37; 'but': 0.37; 'received:org': 0.37; 'steven': 0.38; 'some': 0.38; 'should': 0.38; 'possible,': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'range': 0.60; 'more': 0.61; 'your': 0.61; 'limit': 0.67; 'hand,': 0.76; 'subject:value': 0.84; 'checks.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: verify the return value of a function
Date Fri, 20 Jan 2012 15:42:45 -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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <4f198330$0$29987$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4894.1327092190.27778.python-list@python.org> (permalink)
Lines 19
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327092190 news.xs4all.nl 6936 [2001:888:2000:d::a6]:56048
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19165

Show key headers only | 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