Why doesn't StopIteration get caught in the following code?

grocery_stocker cdalten at gmail.com
Sat Apr 4 17:22:27 CEST 2009


Given the following

[cdalten at localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def counter():
...   mylist = range(3)
...   for i in mylist:
...      yield i*i
...
>>> counter
<function counter at 0xb7e79e64>
>>> gen = counter()
>>> gen
<generator object at 0xb7e7c1cc>
>>> while True:
...    i = gen.next()
...    print i
...
0
1
4
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
StopIteration


I thought the 'for' in counter() was supposed to catch StopIteration.
Ie, I thought that something like

...   for i in mylist:
...      yield i*i

Got translated to something like
>>> try:
...    while True:
...       do some stuff
... except StopIteration:
...    pass
...




More information about the Python-list mailing list