Path: csiph.com!usenet.pasdenom.info!news.redatomik.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; '"""': 0.05; 'cpython': 0.05; 'append': 0.07; 'assignment': 0.07; 'pypy': 0.07; 'undefined': 0.07; '[1,': 0.09; 'iterate': 0.09; 'recommends': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'skip:# 20': 0.13; 'behaviour.': 0.16; 'cc:name:python list': 0.16; 'instead:': 0.16; 'looping.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'looked': 0.16; 'alternate': 0.18; "shouldn't": 0.18; 'specifies': 0.18; 'language': 0.19; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'occurs': 0.22; '2015': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'equivalent': 0.27; 'specify': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'subject:/': 0.29; "i'm": 0.29; 'appending': 0.29; 'behaviour': 0.29; 'checked': 0.31; 'extend': 0.31; "can't": 0.32; 'aside': 0.32; 'url:python': 0.33; "d'aprano": 0.33; 'ram': 0.33; 'steven': 0.33; 'received:google.com': 0.34; 'could': 0.35; 'really': 0.35; 'list': 0.35; 'according': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'skip:i 20': 0.37; 'say': 0.38; 'test': 0.39; 'method': 0.39; 'url:2': 0.39; 'url:docs': 0.39; 'system.': 0.39; 'sure': 0.40; 'skip:t 20': 0.40; 'here.': 0.61; 'times': 0.61; 'here:': 0.62; 'guaranteed': 0.67; 'behave.': 0.84; 'oscar': 0.84; 'subject:good': 0.84; 'try.': 0.91; 'url:tutorial': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=kct0ixqMf4UZTKVuOj+bYFaiCwzbX+B82GIpgDKXc0o=; b=zMvRNR+waIr1S2RDByz6TfpnVrfWFsTGq7GYlMiQJCPo+rC9RYZAdzImxTkbNNmfKL vGgIO9pWQ+TFpfclTUDQ2jv+Poi78AzA/bDg/U1WFrt/WNCYAbURMvM7u49QjwMMdI4U 0TI5K0gZnsIvQvjAdD8M8tjWRzxmDc7A3lSwicP6fa5kZ15YDxL00cUXoa7S5Xw4jGiR PQ34N/BQbbr7MdDtVCG/5X0bZYzLsYnsRjRoW0OnKEzgJysD9WX4NsUUOwclhwM+yV4k foBamNNOftzA66GGobxPkyP5mKvuBLtF1B7HBtv5cQ/9epLaXk7m88+g8wqjuZLkUxwR kboA== X-Received: by 10.181.13.241 with SMTP id fb17mr15795279wid.13.1434199746213; Sat, 13 Jun 2015 05:49:06 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <557bd903$0$11125$c3e8da3@news.astraweb.com> References: <557bd903$0$11125$c3e8da3@news.astraweb.com> From: Oscar Benjamin Date: Sat, 13 Jun 2015 13:48:45 +0100 Subject: Re: zip as iterator and bad/good practices To: "Steven D'Aprano" Cc: Python List Content-Type: text/plain; charset=UTF-8 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434199754 news.xs4all.nl 2967 [2001:888:2000:d::a6]:59706 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92587 On 13 June 2015 at 08:17, Steven D'Aprano wrote: > On Sat, 13 Jun 2015 13:32:59 +0800, jimages wrote: > >> I am a newbie. I also have been confused when I read the tutorial. It >> recommends make a copy before looping. Then I try. >> #-------------------------- >> Test = [1, 2] >> For i in Test: >> Test.append(i) >> #-------------------------- > > You don't make a copy of Test here. You could try this instead: > > Test = [1, 2] > copy_test = Test[:] # [:] makes a slice copy of the whole list > for i in copy_test: # iterate over the copy > Test.append(i) # and append to the original > > print(Test) > > > But an easier way is: > > Test = [1, 2] > Test.extend(Test) > print(Test) I can't see anything in the docs that specify the behaviour that occurs here. If I change it to Test.extend(iter(Test)) then it borks my system in 1s after consuming 8GB of RAM (I recovered with killall python in the tty). According to the docs: """ list.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. """ https://docs.python.org/2/tutorial/datastructures.html#more-on-lists The alternate form Test[len(Test):] = Test is equivalent but Test[len(Test):] = iter(Test) is not since it doesn't bork my system. I looked here: https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types but I don't see anything that specifies how self-referential slice assignment should behave. I checked under pypy and all behaviour is the same but I'm not sure if this shouldn't be considered implementation-defined or undefined behaviour. It's not hard to see how a rearrangement of the list.extend method would lead to a change of behaviour and I can't see that the current behaviour is really guaranteed by the language and in fact it's inconsistent with the docs for list.extend. As an aside they say that pypy is fast but it took about 10 times longer than cpython to bork my system. :) -- Oscar