Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.04; 'nested': 0.07; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'creates': 0.14; 'list)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:recursion': 0.16; 'language': 0.16; 'wrote:': 0.18; 'example': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'lists': 0.32; 'this.': 0.32; 'call.': 0.33; 'anywhere': 0.35; 'created': 0.35; 'but': 0.35; 'doing': 0.36; 'hi,': 0.36; 'clear': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; '2nd': 0.60; 'flat': 0.60; 'new': 0.61; 'received:173': 0.61; 'different': 0.65; '(that': 0.65; '1st': 0.74; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Understanding how is a function evaluated using recursion Date: Wed, 25 Sep 2013 21:12:02 -0400 References: <231e5958-97c9-489c-9cfa-0f4451f6520c@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:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: <231e5958-97c9-489c-9cfa-0f4451f6520c@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380157937 news.xs4all.nl 15883 [2001:888:2000:d::a6]:36868 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54772 On 9/25/2013 7:24 PM, Arturo B wrote: > Hi, I'm doing Python exercises and I need to write a function to flat nested lists > as this one: > > [[1,2,3],4,5,[6,[7,8]]] > > To the result: > > [1,2,3,4,5,6,7,8] > > So I searched for example code and I found this one that uses recursion (that I don't understand): > > def flatten(l): > ret = [] > for i in l: > if isinstance(i, list) or isinstance(i, tuple): > ret.extend(flatten(i)) #How is flatten(i) evaluated? > else: > ret.append(i) > return ret > > So I know what recursion is, but I don't know how is > > flatten(i) > > evaluated, what value does it returns? It is not clear what part of 'how' you do not understand this. Perhaps that fact that a new execution frame with a new set of locals is created for each call. So calling flatten from flatten is no different than call flatten from anywhere else. If a language creates just one execution frame for the function, attached to the function (as with original Fortran, for instance), then recursion is not allowed as a 2nd call would interfere with the use of the locals by the 1st call, etc. -- Terry Jan Reedy