PEP 288: Generator Attributes

logistix logistix at zworg.com
Wed Dec 4 22:34:30 EST 2002


What advantage do Generator Attributes have over a class that
implements the iterator interface?  I always assumed that generators
were just shorthand for an iterable (if thats a word) class.

>>> class foo:
... 	def __iter__(self):
... 		return self
... 	def next(self):
... 		print self.data
... 	def __init__(self):
... 		self.data = 1
... 		
>>> x = iter(foo())
>>> x.next()
1
>>> x.next()
1
>>> x.data = 3
>>> x.next()
3
>>> y = iter(foo())
>>> y.next()
1


Of course Generator Attributes would also be shorthand, but I don't
think it'd be good shorthand.  It'd allow you to do many unobvious
things with your generators.



More information about the Python-list mailing list