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


Groups > comp.lang.python > #50074

Re: Simple recursive sum function | what's the cause of the weird behaviour?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'below).': 0.09; 'except:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'try:': 0.09; 'def': 0.12; 'jan': 0.12; '[])': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sequence:': 0.16; 'sorts': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'code,': 0.22; 'input': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'test.': 0.24; "i've": 0.25; '(see': 0.26; 'pass': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'draft': 0.30; 'work.': 0.31; 'that.': 0.31; 'assert': 0.31; 'subject:what': 0.31; 'subject:the': 0.34; "can't": 0.35; 'except': 0.35; 'case,': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'edge': 0.36; 'subject:Simple': 0.36; 'subject:?': 0.36; 'half': 0.37; 'too': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'catch': 0.60; 'received:173': 0.61; 'simple': 0.61; 'back': 0.62; 'hour': 0.70; 'guaranteed': 0.75; 'yourself': 0.78; 'bare': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Simple recursive sum function | what's the cause of the weird behaviour?
Date Sat, 06 Jul 2013 14:47:27 -0400
References <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.4342.1373136464.3114.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373136464 news.xs4all.nl 15983 [2001:888:2000:d::a6]:43716
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50074

Show key headers only | View raw


On 7/6/2013 8:37 AM, Russel Walker wrote:
> I know this is simple but I've been starring at it for half an hour and trying all sorts of things in the interpreter but I just can't see where it's wrong.
>
> def supersum(sequence, start=0):
>      result = start
>      for item in sequence:
>          try:
>              result += supersum(item, start)
>          except:

Bare except statements cover up too many sins. I and others *strongly* 
recommend that you only catch what you *know* you actually want to (see 
below).

>              result += item
>      return result

I recommend that you start with at least one test case, and with an edge 
case at that. If you cannot bring yourself to do it before writing a 
draft of the function code, do it immediately after and run. If you do 
not want to use a framework, use assert.

assert supersum([]) == 0
assert supersum([], []) == []

Do the asserts match your intention? The tests amount to a specification 
by example. Any 'kind' of input that is not tested is not guaranteed to 
work.

Back to the except clause: only add try..except xxx when needed to pass 
a test.

-- 
Terry Jan Reedy

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


Thread

Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:37 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:54 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:59 -0700
    Re: Simple recursive sum function | what's the cause of the weird behaviour? Peter Otten <__peter__@web.de> - 2013-07-06 15:19 +0200
    Re: Simple recursive sum function | what's the cause of the weird behaviour? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-06 19:43 +0100
      Re: Simple recursive sum function | what's the cause of the weird behaviour? Rotwang <sg552@hotmail.co.uk> - 2013-07-06 21:10 +0100
        Re: Simple recursive sum function | what's the cause of the weird behaviour? Rotwang <sg552@hotmail.co.uk> - 2013-07-06 21:25 +0100
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Chris Angelico <rosuav@gmail.com> - 2013-07-07 03:22 +1000
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Terry Reedy <tjreedy@udel.edu> - 2013-07-06 14:47 -0400
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-07 09:13 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-07 09:44 -0700

csiph-web