Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!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.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'element': 0.07; 'list?': 0.07; '[1,': 0.09; 'append': 0.09; '[])': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'recurse': 0.16; 'sorts': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '>>>>': 0.31; 'subject:what': 0.31; 'not.': 0.33; 'subject:the': 0.34; 'problem': 0.35; "can't": 0.35; 'received:google.com': 0.35; 'subject:Simple': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'kind': 0.63; 'jul': 0.74; 'not:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=k0Hp0OApyhQ6ekf8yzmhXzZChFQ781oscZ9r+eyyjMU=; b=j8wUNQNXAwqSGrGNFqny0bteJtVkUDohePVGD4anW3MwY5Xceyfqyy5jHf1i9naxMl UNOw2k88eKvu1uyToV89TJOddOyhcVr8D+lU3PL/sWUxi8+OsEB6+bFZeFSp/7lUQaLS ao6Xxd5CP78uu5PfLXa2yVafHbx7YtLpbLn6jEw/S8vIMU3RSEg8Yi7x6YHgl8R9avSv v8rIEWS3UbQorj2Yjc/tihjmVZCxXr1CMFfxx8soLYj/2iH8pqUgPeyqKlV0L3s1tpjf QXWTw4pADMlNLYoONQygZBo3qVWCSssrxz8j48msYfwag7/PkFp6fTpyTifu5cQ0B3qT 3glA== MIME-Version: 1.0 X-Received: by 10.220.182.193 with SMTP id cd1mr10508808vcb.32.1373131360714; Sat, 06 Jul 2013 10:22:40 -0700 (PDT) In-Reply-To: References: Date: Sun, 7 Jul 2013 03:22:40 +1000 Subject: Re: Simple recursive sum function | what's the cause of the weird behaviour? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373131363 news.xs4all.nl 15943 [2001:888:2000:d::a6]:57982 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50071 On Sat, Jul 6, 2013 at 10:37 PM, Russel Walker wrote: > This works: > - - - - - - >>>> x = [[1], [2], [3]] >>>> supersum(x) > 6 >>>> supersum(x, []) > [1, 2, 3] >>>> > > > This does not: > - - - - - - - >>>> x = [[[1], [2]], [3]] >>>> supersum(x, []) > [1, 2, 1, 2, 3] >>>> You have a problem of specification here. What should supersum do with the list [1]? Should it recurse into it, or append it as a list? It can't do both. For a list flattener, you would need to either use .append for each element you come across, or .extend with each list, with some kind of check to find whether you should recurse or not. Still, it's a fun thing to play with. I like code golfing these sorts of trinketty functions, just for fun :) ChrisA