Iteratoration question
grocery_stocker
cdalten at gmail.com
Thu Apr 2 18:37:16 EDT 2009
>
> in summary: iterator is bound to one instance of "it", while some_func()
> returns a new instance each time it is called.
>
> BUT
>
> while what you are doing is interesting, it is not the same as Python's
> iterators, which use "yield" from a function and don't require storing a
> value in a class. look for "yield" in the python docs. this comment may
> be irrelevant; i am just worried you are confusing the above (which apart
> from the mistake about instances is perfectly ok) and python's iterators
> (which use next(), yield, etc).
>
Okay, one last question for now
When I have the follow class
>>> class it:
... def __init__(self):
... self.count = -1
... def next(self):
... self.count +=1
... if self.count < 4:
... return self.count
... else:
... raise StopIteraton
...
>>> value = it()
How comes I can;t go over 'value' like in the following
>>> for x in value:
... print x
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence
But yet, I can do...
>>> value.next()
0
>>> value.next()
1
>>> value.next()
2
>>> value.next()
3
>>>
More information about the Python-list
mailing list