Python 2.6.2
>>> call_iter = iter(lambda x: x + 1, 100)
>>> call_iter.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (0 given)
I want to pass argument to lambda x:x + 1
.
Update: I think the example above is hard to understand.
I wonder whether there is a builtin func like myiter in python:
class myiter:
def __init__(self, callable, initial, sentinel):
self.value = initial
self.callable = callable
self.sentinel = sentinel
def __iter__(self):
return self
def next(self):
if self.value == self.sentinel:
raise StopIteration
else:
# calculate next value from prev value
self.value = self.callable(self.value)
return self.value
if __name__ == '__main__':
call_iter = myiter(lambda x:x + 1, 0, 100)
for i in call_iter:
print i