Python Iteration Protocol: Lazy Evaluation with __iter__ and __next__

Why range(1,000,000) is cheap, but list(range(1,000,000)) is costly in Python? TL;DR: Iteration Protocol in Python needs to know only next item and not full list. The "Next Page" Rule Iteration in Python isn't about having a collection of items; it’s about knowing how to get the next item. Two special methods make this possible: 1. __iter__() → tells Python “I can be looped over” 2. __next__() → returns the next value, one at a time When there’s nothing left, StopIteration tells Python to stop the loop. Why this matters? When we use a list, we pay for all the memory upfront. When we use the Iteration Protocol, we only pay for one item at a time. This is called Lazy Evaluation. Takeaway - If the object represents a collection or a stream of data, implement __iter__ and __next__. It makes the code more memory-efficient and much more "Pythonic." I’m deep-diving into the Python protocols this week and will share my learnings. Do follow along and tell your experiences in comments. #Python #PythonInternals #SoftwareEngineering #BackendDevelopment

  • text

List conversion requires a bulk of data to to be stored as a whole at a time. range is a generator function using which such large data can be fetched as a stream as per the need one at a time. So thats why memory requirements are lesser... Good posts..

To view or add a comment, sign in

Explore content categories