I am trying to understand iterators and iterables. I have the python program below.
class fib():
def __init__(self):
self.prev = 0
self.curr = 1
def __iter__(self):
return self
def __next__(self):
value = self.curr
self.curr += self.prev
self.prev = value
return value
f = fib()
print(type(f))
z = iter(f)
item = z.next()
print(z)
item = z.next()
print(z)
I get the following error
item = z.next()
AttributeError: 'fib' object has no attribute 'next'
1
What am I doing wrong and why am I getting this error.As per my understanding calss fib() is an iterable
and hence should return an iterator.Calling next() on that iterator should return the items.