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: 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 Subject: Re: Simple recursive sum function | what's the cause of the weird behaviour? Date: Sat, 06 Jul 2013 14:47:27 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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