What happens when `__iter__` returns `self` in a class?

Python Professional Hard

Python Professional — Hard

What happens when `__iter__` returns `self` in a class?

Key points

  • Returning `self` in `__iter__` method makes the object an iterable and an iterator
  • It allows the object to be used in a for loop without needing to create a separate iterator
  • The object can be iterated over multiple times in different loops

Ready to go further?

Related questions