Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100124
| From | Robin Koch <robin.koch@t-online.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Help on for loop understanding |
| Date | 2015-12-08 02:14 +0100 |
| Organization | albasani.net |
| Message-ID | <n45atn$2qs$1@news.albasani.net> (permalink) |
| References | <f9ae2857-2ae2-4fe2-8547-d57e5ebe1fe5@googlegroups.com> |
Am 08.12.2015 um 02:05 schrieb Robert: > 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? Lists don't have a next method. Their iterators have: xx.__iter__().__next__() or xxIterator = xx.__iter__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() xxIterator.__next__() That's also what your quoted paragraph states: | It then repeatedly calls the next() method | (__next__() method in Python 3.x) of the iterator -- Robin Koch
Back to comp.lang.python | Previous | Next — Previous in thread | 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