Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Erik Newsgroups: comp.lang.python Subject: Re: Help on for loop understanding Date: Tue, 8 Dec 2015 01:53:34 +0000 Lines: 45 Message-ID: References: <5d02c61f-a979-4121-a9ae-764b44903156@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de buySHZ0iUUP1Shnb5u9iqgwK23xRLtPGZNFNzA2WowvQ== 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; 'yet.': 0.03; '(python': 0.05; '(so': 0.07; '(using': 0.07; 'exception.': 0.07; 'list?': 0.07; 'raises': 0.07; 'shortcut': 0.07; '[1,': 0.09; 'exits': 0.09; 'loop.': 0.09; 'seen,': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'exception': 0.13; 'ignore': 0.14; 'explicitly': 0.15; 'from:addr:python': 0.16; 'iterated': 0.16; 'iterator': 0.16; 'keyword,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'robert,': 0.16; 'suppresses': 0.16; 'wrote:': 0.16; 'element': 0.18; 'versions': 0.20; 'to:2**1': 0.21; 'bit': 0.23; 'thanks,': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:skip:u 10': 0.29; 'classes': 0.30; 'probably': 0.31; 'received:84': 0.32; 'instead,': 0.33; 'running': 0.34; 'list': 0.34; 'gets': 0.35; 'robert': 0.35; 'but': 0.36; 'should': 0.36; 'keyword': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'things': 0.38; 'doing': 0.38; 'version': 0.38; 'why': 0.39; 'received:192': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'show': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'different': 0.63; 'detail.': 0.66; 'directly.': 0.76; "'for'": 0.84; 'hood': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JN/GyJ+b c=1 sm=1 tr=0 a=Ypmeq7T0cKALDUsRPCToMg==:117 a=Ypmeq7T0cKALDUsRPCToMg==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=N659UExz7-8A:10 a=_g4AsR0aHoPmyIAnfesA:9 a=pILNOxqGKmIA:10 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <5d02c61f-a979-4121-a9ae-764b44903156@googlegroups.com> 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: , Xref: csiph.com comp.lang.python:100128 Hi Robert, On 08/12/15 01:39, Robert wrote: >> I don't find a way to show __next__ yet. >> Can we explicitly get the iterator for a list? >> Thanks, > > Excuse me. I find it as the following: > > xx.__iter__().next > Out[16]: > > xx.__iter__().next() > Out[17]: 1 Robin has told you how things work under the hood for the particular version of Python that he is running (Python 3). As you've seen, it works a bit different under the hood in your version (Python 2). This is why you should not be calling the __ ("dunder") methods directly. Until you understand more and want to write your own classes that support being iterated using the 'for' keyword, you should probably ignore them. Instead, the way to do this which works on all versions is: x = [1, 2, 3, 4] xit = iter(x) next(xit) next(xit) next(xit) next(xit) next(xit) This is what the 'for' keyword is doing for you - it first gets an iterator for the list (using iter()) and then processes each element that the iterator returns (from next()) until it raises the exception. It then suppresses the exception (so you don't have to catch it yourself) and exits the for loop. Of course, it might actually do this using the __ methods and other things as a shortcut internally, but that's just an implementation detail. E.