I have a class like this
class MyClass:
def __init__(self):
self.test = [1,2,3]
Now I want to iterate over the test
property like this:
a = MyClass()
for i in a:
print(I)
So I've added another method to the class:
def __iter__(self):
return self.test
Which doesn't work, unless I instead do this:
return iter(self.test)
I thought the for
loop implicitly calls iter()
behind the scenes and it's obviously capable of iterating over a normal list if I would do for i in a.test
so obviously I'm not understanding something about using those functions. Would love some clarification