Finally figured out member generators...

Jeff Epler jepler at unpythonic.net
Mon Mar 3 14:32:22 EST 2003


Which version of Python are you using?

I just tried a similar example on Python 2.2.2 using "from __future__
import generators", and there was no need to introduce a separate
method, or call it in a strange way.

>>> from __future__ import generators
>>> class R10:
...     def __iter__(self):   
...             for i in range(10): yield i
... 
>>> list(R10())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> R10().__iter__
<bound method R10.__iter__ of <__main__.R10 instance at 0x4008088c>>
>>> R10.__iter__
<unbound method R10.__iter__>
>>> r = R10()
>>> list(r.__iter__())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(R10.__iter__(r))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> iter(r) 
<generator object at 0x40066c00>

Jeff





More information about the Python-list mailing list