The easiest way to do this seemed to be to write a class, like so:
class Iterator:
def __init__(self, list):
self.list=list
self.pos=-1
return self.generator
def generator(self):
self.pos = self.pos + 1
return self.list[self.pos]
Thus, when the user called Iterator with a list:
Iterator([1,2,3,4,5])
This would then initialize the instance with the list, and return
an instance method (not the instance itself). This method (generator)
would then return 1, 2, etc.
>>> x = Iterator([1,2,3,4,5])
>>> x()
1
>>> x()
2
Unfortunately, Python requires that __init__ return None.
What I propose is that the user should be able to override the default
return value of __init__, by explicitly returning a value other than
None.
I know that I can just write another function to return the generator,
but then my creation syntax becomes:
>>> x = Iterator().make([1,2,3,4,5])
which is not as pretty.
BTW: to add fuel to the syntax debate, I taught an "introduction to C"
lab last semester for the first time. I cannot tell you how many times
student's code wasn't doing what they wanted because they got the braces
wrong, like so:
if (x==3) {
do_this; }
do_this_too;
Notice that the indentation was correct...
Steve
-- Steven Miale - smiale@cs.indiana.edu | Don't blame me - Indiana University, Bloomington, IN | I voted Libertarian.