0

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?

0

1 Answer 1

6

If you implement an __iter__() method, you can do this functionality:

Code:

class MockList:

    def __iter__(self):
        return iter(range(1, 6))

Test Code:

for i in MockList():
    print(i)

Results:

1
2
3
4
5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.