🚀 Turning ☕ into code
learning 🌱 daily
https://github.com/Jean-EstevezT/
However, isn't Generators and Lazy Evaluation a crucial skill precisely for knowing when and how to optimize when the 'problem' is memory or datasets that don't fit in RAM?
However, isn't Generators and Lazy Evaluation a crucial skill precisely for knowing when and how to optimize when the 'problem' is memory or datasets that don't fit in RAM?
Create an infinite Fibonacci sequence generator (while True + yield).
Use it to find the first number with more than 1000 digits.
Are you up for the challenge? Paste your solution in the comments. 👇
#PythonLearning #Backend #DevCommunity
Create an infinite Fibonacci sequence generator (while True + yield).
Use it to find the first number with more than 1000 digits.
Are you up for the challenge? Paste your solution in the comments. 👇
#PythonLearning #Backend #DevCommunity
⚠️ CAUTION: They are for single use only.
⚠️ CAUTION: They are for single use only.
One generator calling another?
Before, you had to create manual loops. With `yield from`, a main generator delegates control to a sub-generator.
It's the "grandfather" of modern `async`/`await` and vital for understanding asynchronicity
One generator calling another?
Before, you had to create manual loops. With `yield from`, a main generator delegates control to a sub-generator.
It's the "grandfather" of modern `async`/`await` and vital for understanding asynchronicity
Generators don't just "speak" (yield output), they also "listen."
With .send(), you can inject data into the generator while it's running. This turns them into coroutines.
Useful for state machines or changing the flow's behavior in real time.
Generators don't just "speak" (yield output), they also "listen."
With .send(), you can inject data into the generator while it's running. This turns them into coroutines.
Useful for state machines or changing the flow's behavior in real time.
Want to analyze 50GB logs without crashing the server? Chain generators together.
Read file ➡️ Filter errors ➡️ Transform data
Each step is a generator. Data flows one by one like water in a pipe, not like a block of cement. 💧
Want to analyze 50GB logs without crashing the server? Chain generators together.
Read file ➡️ Filter errors ➡️ Transform data
Each step is a generator. Data flows one by one like water in a pipe, not like a block of cement. 💧
Imagine processing 100 million records.
📉 List: [x*2 for x in range(10**8)]
Result: Your RAM explodes. PC crashes.
📈 Generator: (x*2 for x in range(10**8))
Result: Occupies almost 0 bytes. Generates data only when needed.
[] = Container.
() = Recipe
Imagine processing 100 million records.
📉 List: [x*2 for x in range(10**8)]
Result: Your RAM explodes. PC crashes.
📈 Generator: (x*2 for x in range(10**8))
Result: Occupies almost 0 bytes. Generates data only when needed.
[] = Container.
() = Recipe
When you call a generator, Python doesn't execute the code. It returns a "generator" object.
The code only runs when you use `next()`.
It runs until `yield` -> It gives you the data -> It pauses.
If there's no more `yield`, it calls `StopIteration`
When you call a generator, Python doesn't execute the code. It returns a "generator" object.
The code only runs when you use `next()`.
It runs until `yield` -> It gives you the data -> It pauses.
If there's no more `yield`, it calls `StopIteration`
❌ Normal (Eager): Creates the entire list in memory before returning it.
✅ Generator (Lazy):
He doesn't do anything until you ask him to.
❌ Normal (Eager): Creates the entire list in memory before returning it.
✅ Generator (Lazy):
He doesn't do anything until you ask him to.
Normal functions run until they terminate (return).
A Generator can pause, deliver a value, maintain its state, and resume where it left off.
The key is the word: yield.
Normal functions run until they terminate (return).
A Generator can pause, deliver a value, maintain its state, and resume where it left off.
The key is the word: yield.
Here is the link of the project
github.com/Jean-Estevez...
Here is the link of the project
github.com/Jean-Estevez...