Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100123
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-12-07 17:05 -0800 |
| Message-ID | <f9ae2857-2ae2-4fe2-8547-d57e5ebe1fe5@googlegroups.com> (permalink) |
| Subject | Help on for loop understanding |
| From | Robert <rxjwg98@gmail.com> |
Hi, When I learn for loop with below link: http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html it has such explanation: \\\\\\\\\ for loop under the hood First let's look at the for loop under the hood. When Python executes the for loop, it first invokes the __iter__() method of the container to get the iterator of the container. It then repeatedly calls the next() method (__next__() method in Python 3.x) of the iterator until the iterator raises a StopIteration exception. Once the exception is raised, the for loop ends. \\\\\\\\\ When I follow a list example from the above link, and one example of myself: ////////// xx=[1,2,3,4,5] xx.next --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-76-dd0716c641b1> in <module>() ----> 1 xx.next AttributeError: 'list' object has no attribute 'next' xx.__iter__ Out[77]: <method-wrapper '__iter__' of list object at 0x000000000A1ACE08> for c in xx: print c 1 2 3 4 5 ////////////// I am puzzled that the list examples have no next method, but it can run as a for loop. Could you explain it to me the difference? Thanks,
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Help on for loop understanding Robert <rxjwg98@gmail.com> - 2015-12-07 17:05 -0800
Re: Help on for loop understanding Robin Koch <robin.koch@t-online.de> - 2015-12-08 02:14 +0100
Re: Help on for loop understanding Robert <rxjwg98@gmail.com> - 2015-12-07 17:31 -0800
Re: Help on for loop understanding Robert <rxjwg98@gmail.com> - 2015-12-07 17:39 -0800
Re: Help on for loop understanding Robert <rxjwg98@gmail.com> - 2015-12-07 17:50 -0800
Re: Help on for loop understanding Erik <python@lucidity.plus.com> - 2015-12-08 02:36 +0000
Re: Help on for loop understanding Chris Angelico <rosuav@gmail.com> - 2015-12-08 14:23 +1100
Re: Help on for loop understanding Robert <rxjwg98@gmail.com> - 2015-12-08 06:59 -0800
Re: Help on for loop understanding Erik <python@lucidity.plus.com> - 2015-12-08 01:53 +0000
csiph-web