Bird
0
0

Why does an iterator's __iter__ method usually return self instead of a new object?

hard📝 Conceptual Q10 of 15
Python - Magic Methods and Operator Overloading
Why does an iterator's __iter__ method usually return self instead of a new object?
ABecause __iter__ must always return a new instance
BTo create a fresh iterator each time iter() is called
CTo maintain the current iteration state within the same object
DTo reset the iteration counter automatically
Step-by-Step Solution
Solution:
  1. Step 1: Understand iterator state

    An iterator keeps track of its position internally. Returning self preserves this state.
  2. Step 2: Analyze alternatives

    Returning a new object would reset iteration, breaking expected behavior. __iter__ does not reset automatically.
  3. Final Answer:

    To maintain the current iteration state within the same object -> Option C
  4. Quick Check:

    __iter__ returns self to keep iteration state [OK]
Quick Trick: Return self in __iter__ to keep iteration state [OK]
Common Mistakes:
  • Thinking __iter__ resets iteration
  • Returning new object each time __iter__ called
  • Confusing iterable with iterator behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes