Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'guido': 0.05; 'removes': 0.05; 'undefined': 0.07; 'caller.': 0.09; 'implies': 0.09; 'mess': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spec': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; 'stack': 0.13; 'wed,': 0.15; 'explicitly': 0.15; 'cares': 0.16; 'evaluates': 0.16; 'executed.': 0.16; 'none),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'similarly,': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'passes': 0.18; 'say,': 0.18; 'try:': 0.18; '2015': 0.20; 'doc': 0.22; 'am,': 0.23; '(or': 0.23; 'header:In-Reply-To:1': 0.24; 'header :User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'helpful': 0.27; 'function': 0.28; 'fine': 0.28; 'about.': 0.29; 'behaviour': 0.29; 'tail': 0.29; 'read,': 0.29; 'subject:/': 0.30; 'code': 0.30; '15,': 0.30; 'entry': 0.31; 'everyone': 0.31; 'post': 0.31; 'statement': 0.32; 'url:python': 0.33; 'call,': 0.33; 'traceback': 0.33; 'except': 0.34; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'evaluation': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'anything': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'url:3': 0.60; 'behavior': 0.61; 'back': 0.62; 'linked': 0.63; 'more': 0.63; 'within': 0.64; 'url:co': 0.65; 'statement,': 0.66; 'therefore': 0.67; 'stated': 0.70; 'jul': 0.72; 'present.': 0.72; 'construct': 0.84; 'received:fios.verizon.net': 0.91; 'url:reference': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Date: Tue, 14 Jul 2015 21:01:21 -0400 References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> <877fq3nuwo.fsf@elektro.pacujo.net> <87pp3vm93f.fsf@elektro.pacujo.net> <55a54818$0$1652$c3e8da3$5496439d@news.astraweb.com> <87vbdmd48e.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436922114 news.xs4all.nl 2833 [2001:888:2000:d::a6]:50698 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5736 X-Received-Body-CRC: 3887171738 Xref: csiph.com comp.lang.python:93841 On 7/14/2015 1:52 PM, Chris Angelico wrote: > On Wed, Jul 15, 2015 at 3:43 AM, Marko Rauhamaa wrote: >> I don't like the way integer overflows are explicitly undefined in >> modern C. >> >> Similarly, I don't like the way tail call behavior is undefined in >> Python. > > Where in the Python spec is it stated that tail call behaviour is > undefined? The behaviour of the 'return' statement is well defined: it > evaluates its expression (or None), *then* removes the top of the call > stack and passes control back to the caller: > > https://docs.python.org/3/reference/simple_stmts.html#the-return-statement I do not see anything there explicitly about call stacks. > This implies that during the evaluation of its expression, the current > function's call stack entry is still present. Tail call behaviour is > therefore well defined: it is identical to any other expression > evaluation, and then the final result is passed back to the caller. In the blog post http://neopythonic.blogspot.co.uk/2009/04/tail-recursion-elimination.html that everyone discussing this issue should read, Guido said that if a tail call is not within a try statement, tco would be fine except for the implementation issue of helpful tracebacks, which he cares greatly about. However, as both he and the linked doc say, a tail call within a try: statement is not a tail call, in that more python code within the function may yet be executed. It is not hard to construct an example in which tco would mess up a traceback message that is displayed. -- Terry Jan Reedy