Your __iter__
method IS returning an object with a next
function:
z = Vector2d(4, 5)
z_iter = z.__iter__()
print(type(z_iter))
for coord in z:
print(coord)
# <type 'generator'>
It's this generator that's providing the next()
func.
Here's a very silly re-write of your vector class:
class Vector2d:
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
self.index = 0
def __iter__(self):
return self
def next(self):
if self.index < 2:
ret = [self.x, self.y][self.index]
self.index += 1
return ret
else:
raise StopIteration()
v = Vector2d(1, 2)
for coord in v:
print(coord)
This does actually provide native iteration functionality - again, in a very silly way.
edit: replace next()
with __next__()
in older python 2.x versions. I forget which exactly.