Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'mrab': 0.05; "'',": 0.07; 'compiler': 0.07; 'correct.': 0.07; 'made.': 0.07; 'referring': 0.07; '3),': 0.09; '[1,': 0.09; 'compile-time': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sentence': 0.09; 'stack.': 0.09; 'subject:while': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'template': 0.14; '(1,': 0.16; '(none,': 0.16; 'compiler.': 0.16; 'creation.': 0.16; 'immutable,': 0.16; 'letting': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'return,': 0.16; 'subject:recursion': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'items.': 0.19; 'examples': 0.20; 'not,': 0.20; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'copied': 0.24; 'looks': 0.24; 'developers': 0.25; 'compiled': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'included': 0.31; 'code': 0.31; 'that.': 0.31; '>>>>': 0.31; 'constant': 0.31; 'context,': 0.31; 'context.': 0.31; "d'aprano": 0.31; 'loads': 0.31; 'object.': 0.31; 'steven': 0.31; 'tuples': 0.31; 'types.': 0.31; 'quite': 0.32; 'not.': 0.33; 'sense': 0.34; 'core': 0.34; 'could': 0.34; 'created': 0.35; 'objects': 0.35; 'but': 0.35; 'next': 0.36; 'two': 0.37; 'list': 0.37; 'lists.': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'analyze': 0.60; 'dave': 0.60; 'is.': 0.60; 'received:173': 0.61; 'simply': 0.61; 'first': 0.61; 'save': 0.62; 'here': 0.66; 'default': 0.69; 'conservative': 0.84; 'received:fios.verizon.net': 0.84; 'angel': 0.91; 'not:': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Tail recursion to while iteration in 2 easy steps Date: Wed, 02 Oct 2013 22:41:00 -0400 References: <87had0axxy.fsf@dpt-info.u-strasbg.fr> <524cc73a$0$29984$c3e8da3$5496439d@news.astraweb.com> <524CCC8D.6020700@mrabarnett.plus.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: <524CCC8D.6020700@mrabarnett.plus.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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380768081 news.xs4all.nl 15871 [2001:888:2000:d::a6]:52136 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55400 On 10/2/2013 9:46 PM, MRAB wrote: > On 03/10/2013 02:39, Dave Angel wrote: >> On 2/10/2013 21:24, Steven D'Aprano wrote: >> >>> On Wed, 02 Oct 2013 18:17:06 -0400, Terry Reedy wrote: >>> >>>> CPython core developers have be very conservative about what >>>> tranformations they put into the compiler. (1,2,3) can always be >>>> compiled as a constant, and so it is. [1,2,3] might or might not be a >>>> constant, depending on the context, and no attempt is made to analyze >>>> that. To be specific: in for i in [1,2,3]: print i it looks like it might be save to pre-compile the list and just use it when the code is run. But if the above is in a function object whose code object is ever introspected, the list object could be accessed and mutated. Letting the compiler know that it can do the optimization is one reason to use tuples in situations like the above. >>> The first sentence of this is correct. The next two don't quite make >>> sense to me, since I don't understand what you mean by "constant" in >>> this context. >>> I *think* you might be referring to the LOAD_CONST byte-code, I am referring to constant-value objects included in the code object. >>> def f(): return (1,2,3) >>> f.__code__.co_consts (None, 1, 2, 3, (1, 2, 3)) None is present as the default return, even if not needed for a particular function. Every literal is also tossed in, whether needed or not. >>> which in Python 3.3 understands tuples like (1, 2, 3), but not lists. The byte-code does not understand anything about types. LOAD_CONST n simply loads the (n+1)st object in .co_consts onto the top of the stack. >>> S9 a literal (1, 2, 3) gets created at compile-time with a single >>> LOAD_CONST call: >>> >>> py> from dis import dis >>> py> dis(compile("x = (1, 2, 3)", '', 'exec')) >>> 1 0 LOAD_CONST 4 ((1, 2, 3)) >>> 3 STORE_NAME 0 (x) >>> 6 LOAD_CONST 3 (None) >>> 9 RETURN_VALUE >>> >>> >>> while a literal [1, 2, 3] does not: >>> >> >> The difference is that a tuple can be reused, so it makes sense for the >> comiler to produce it as a const. (Much like the interning of small >> integers) The list, however, would always have to be copied from the >> compile-time object. So that object itself would be a phantom, used >> only as the template with which the list is to be made. >> > The key point here is that the tuple is immutable, including its items. The items of the tuple I gave as an examples are all constant. If they were not, the tuple would not be a constant for the purpose of compile-time creation. -- Terry Jan Reedy