On Monday, December 7, 2015 at 8:32:30 PM UTC-5, Robert wrote: > On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote: > > 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 > > I use Python 2.7. I have tried these commands: > > xx=[1,2,3,4,5] > > xx.__iter__ > Out[2]: <method-wrapper '__iter__' of list object at 0x000000000A1B85C8> > > xx.__iter__().__next__() > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > <ipython-input-3-980bcf1bbf42> in <module>() > ----> 1 xx.__iter__().__next__() > > AttributeError: 'listiterator' object has no attribute '__next__' > > xx.__iter__.__next__ > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > <ipython-input-4-df8d8c6955e2> in <module>() > ----> 1 xx.__iter__.__next__ > > AttributeError: 'method-wrapper' object has no attribute '__next__' > > xx.__iter__() > Out[5]: <listiterator at 0xa211780> > > xx.__iter__().__next__ > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > <ipython-input-6-92f21313d62e> in <module>() > ----> 1 xx.__iter__().__next__ > > AttributeError: 'listiterator' object has no attribute '__next__' > > xxIterator = xx.__iter__() > > xxIterator > Out[8]: <listiterator at 0xa2115c0> > > xxIterator.__next__() > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > <ipython-input-9-5b74e35c2c6e> in <module>() > ----> 1 xxIterator.__next__() > > AttributeError: 'listiterator' object has no attribute '__next__' > > for c in xx: print c > 1 > 2 > 3 > 4 > 5 > ------------ > > 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]: <method-wrapper 'next' of listiterator object at 0x0000000008B38AC8> xx.__iter__().next() Out[17]: 1 -- https://mail.python.org/mailman/listinfo/python-list