Reputation: 199
I was asked this question. Given the code:
class MockList:
...code_here...
for i in MockList():
print(i)
Expected result of for loop:
1 2 3 4 5
How can I do this?
Upvotes: 0
Views: 56
Reputation: 49812
If you implement an __iter__()
method, you can do this functionality:
class MockList:
def __iter__(self):
return iter(range(1, 6))
for i in MockList():
print(i)
1
2
3
4
5
Upvotes: 6