Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'compiler': 0.07; 'detect': 0.07; 'subject:while': 0.09; 'def': 0.12; '(1,': 0.16; 'called,': 0.16; 'dangerous,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'lists).': 0.16; 'subject:recursion': 0.16; 'tuple': 0.16; 'tuple.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'tuples': 0.31; "can't": 0.35; '(2)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'done': 0.36; '(3)': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'course': 0.61; 'compare:': 0.84; 'done:': 0.84; '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=LrTqtsCAruvk2YgLP5C/NeY3drWljws2jF/Icl2IF4U=; b=D8eR6q3TQBFjbqo8YIfN2soehnydeexvWksYafDUFbd+bA+F7jkjQZUFTaHjQ84++9 4rNQhRYVWzpekiGszc5CYiIWw3ZVqU7inN25mmu3/iiTpiLXWQ01dQ+bvKId8+7HwoMR B6dHRBXr9MwXkc9ws7BRjLInimJYyZLQxckLCH0jGs1VDInT0sXsTr9oLvmuoyu1Uxsu X4nzYy1AC+UXhMRNHcC3i7pJg+Oai1KEm8Mlk4gqbQWwfNX0DPXYjwApa7hrB3Wn3qR+ K7THCp3qyDdlAg2Wop9YCwNRvQM6QwkVZEq6YaS2399vXGVjbGPhfvlEgt8GJMMqmZR9 mV7Q== MIME-Version: 1.0 X-Received: by 10.66.25.173 with SMTP id d13mr6797895pag.146.1380773667824; Wed, 02 Oct 2013 21:14:27 -0700 (PDT) In-Reply-To: <524cd7a7$0$29984$c3e8da3$5496439d@news.astraweb.com> References: <87had0axxy.fsf@dpt-info.u-strasbg.fr> <524cc73a$0$29984$c3e8da3$5496439d@news.astraweb.com> <524cd7a7$0$29984$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 3 Oct 2013 14:14:27 +1000 Subject: Re: Tail recursion to while iteration in 2 easy steps 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380773671 news.xs4all.nl 15918 [2001:888:2000:d::a6]:37269 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55403 On Thu, Oct 3, 2013 at 12:34 PM, Steven D'Aprano wrote: > py> def f(): > ... a = (1, 2, 3) > ... b = (1, 2, 3) > ... return a is b > ... > py> f() # Are the tuples the same object? > False That just means the compiler doesn't detect reuse of the same tuple. But compare: >>> def f(): return (1,2,3) >>> f() is f() True Every time the function's called, it returns the same tuple (which obviously can't be done with lists). And of course if that would be dangerous, it's not done: >>> def f(): return (1,[2],3) >>> f()[1].append("Hello") >>> f() (1, [2], 3) >>> import dis >>> dis.dis(f) 2 0 LOAD_CONST 1 (1) 3 LOAD_CONST 2 (2) 6 BUILD_LIST 1 9 LOAD_CONST 3 (3) 12 BUILD_TUPLE 3 15 RETURN_VALUE ChrisA