On Fri, 03 Apr 2009 02:07:38 +0100, grocery_stocker <cdal...@gmail.com>
wrote:
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
...
1
2
3
4
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
translates to:
a = [1,2,3,4]
i = iter(a)
try:
while True:
x = i.next()
print x
except StopIteration:
pass
--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list