rhodri at wildebst.demon.co.uk
Fri Apr 3 03:33:23 CEST 2009
On Fri, 03 Apr 2009 02:07:38 +0100, grocery_stocker <cdalten at gmail.com>
> Okay, I was thinking more about this. I think this is also what is
> irking me. Say I have the following..
>>>> a = [1,2,3,4]
>>>> for x in a:
> ... print x
> Would 'a' somehow call __iter__ and next()? If so, does python just
> perform this magically?
No. It's "for" that invokes the iteration protocol; that's pretty
much the definition of it. You have read the iteration protocol
after it's been mentioned so many times now, haven't you?
"for" calls iter(a), which in turn calls a.__iter__(), to get an
iterator. Once it's got, "for" calls next() on the iterator each
time round the loop. Very approximately, that little for-loop
a = [1,2,3,4]
i = iter(a)
x = i.next()
Rhodri James *-* Wildebeeste Herder to the Masses
More information about the Python-list