Infinite "Generators" - The Never-Empty Akshaya Patra
- Vinay Borhade

- Sep 13
- 2 min read
Miraculous abundance from ancient myth finds its parallel in the endless power of Python generators!
By drawing parallels between ancient stories and modern code, programming concepts become much more relatable, fun and easy to remember!
Akshaya Patra: The Pot That Never Empties
In the great Indian epic, the Mahabharata, the Pandavas receive a miraculous gift during their exile: the Akshaya Patra. This magical vessel, given by the sun god, provides an endless supply of food—no matter how many guests arrive or how much is taken, the pot renews itself again and again, until Draupadi (the day’s chef) is satisfied. The vessel’s abundance is legendary; its promise is clear: “Take as much as you want, there is always more.”
Generative Wonders in Python
While Python offers no divine cookware, it gives us the next best thing for data: infinite generators. With a simple function and a while True loop, you can create a “pot” that never runs out—producing as much data or as many values as you want, only pausing when you choose to stop drawing.
Generators are a magical Python feature allowing you to:
Produce sequences lazily—item by item, as needed—without ever storing the entire sequence in memory.
Build endless streams for simulations, unique IDs, or even serving up random data for hungry code!
Story to Code
Suppose you have a list of fruits and want to “serve” them one by one each time someone asks—rather than handing out the whole basket at once.
def serve_fruits(fruit_list):
for fruit in fruit_list:
yield f"Serving: {fruit}"
# The fruit "Akshaya Patra"
basket = ["mango", "banana", "guava", "papaya", "jackfruit"]
# Get the generator
fruit_server = serve_fruits(basket)
# Serve fruits one at a time (could be in a loop)
print(next(fruit_server)) # Serving: mango
print(next(fruit_server)) # Serving: banana
print(next(fruit_server)) # Serving: guava
# ...and so on until the basket is empty
Real world use cases for generators
1. File Processing (Reading Big Files Line by Line)
Instead of loading massive files into memory, generators allow you to read and process each line or chunk as needed, for example when parsing logs or CSVs.
def read_large_file(filename):
with open(filename) as f:
for line in f:
yield line.strip()2. Streaming Data or APIs (Lazy Fetching Results)
Generators help you process streaming data (like rows from a database cursor or API responses) one item at a time, using very little memory even for huge datasets.
3. Real-Time Sensor or IoT Data
If you receive data from sensors or hardware events (like temperature readings), a generator can yield each value for immediate processing.
4. Lazy Image or Video Processing
Yield one frame or image at a time from a very large set, for real-time applications or streaming.
Why is this generator powerful?
You only get the next fruit when you ask—no need to load all at once.
It’s memory efficient for very large data.
Easily fits real-world tasks, like reading files line-by-line, streaming data, or responding to user requests one at a time.
Just like Akshaya Patra, your generator “serves” each value exactly when it’s requested, ensuring nothing is wasted or left waiting!



Comments