🤔 Python generators

What are they? How to use them?

#Python

🧵Let's find out 👇

1️⃣ Python generators are lazy iterators delivering the next value when their .next() is called.

They are created by using the yield keyword

next() can be called explicitly or implicitly inside for loop

They can be finite or infinite
2️⃣ yield - where a value is sent back to the caller, but the function doesn’t exit afterward as with the return statement

The state of function is remembered.

For example, the number is incremented and sent back from yield at the consecutive call next()
3️⃣ Generator stores only the current state of the function - it generates next element on next() call and forgets the previous one -> it saves memory

For example, we don't need to store 1mio elements in memory to do something with each element
4️⃣ When you call a generator function generator object is returned - it's not executed yet

It executes only when next() is called

For example, that's why Exception is raised only on the next() call
5️⃣ When the generator goes out of elements it raises StopIteration exception
6️⃣ You can also create a class that behaves like a generator - it needs implemented methods:

- __iter__ -> to enable iteration
- __next__ -> to enable next element access
7️⃣ You can also create a generator with one-liner expression similar to list comprehension
8️⃣ You can read more:

https://t.co/mXp7L8l2Ai

More from Machine learning

You May Also Like

क्या आप जानते हैं कि क्या है, पितृ पक्ष में कौवे को खाना देने के पीछे का वैज्ञानिक कारण!

श्राद्ध पक्ष में कौओं का बड़ा ही महत्व है। कहते है कौआ यम का प्रतीक है, यदि आपके हाथों दिया गया भोजन ग्रहण कर ले, तो ऐसा माना जाता है कि पितरों की कृपा आपके ऊपर है और वे आपसे ख़ुश है।


कुछ लोग कहते हैं की व्यक्ति मरकर सबसे पहले कौवे के रूप में जन्म लेता है और उसे खाना खिलाने से वह भोजन पितरों को मिलता है

शायद हम सबने अपने घर के किसी बड़े बुज़ुर्ग, किसी पंडित या ज्योतिषाचार्य से ये सुना होगा। वे अनगिनत किस्से सुनाएंगे, कहेंगे बड़े बुज़ुर्ग कह गए इसीलिए ऐसा करना

शायद ही हमें कोई इसके पीछे का वैज्ञानिक कारण बता सके।

हमारे ऋषि मुनि और पौराणिक काल में रहने वाले लोग मुर्ख नहीं थे! कभी सोचियेगा कौवों को पितृ पक्ष में खिलाई खीर हमारे पूर्वजों तक कैसे पहुंचेगी?

हमारे ऋषि मुनि विद्वान थे, वे जो बात करते या कहते थे उसके पीछे कोई न कोई वैज्ञानिक कारण छुपा होता था।

एक बहुत रोचक तथ्य है पितृ पक्ष, भादो( भाद्रपद) प्रकृति और काक के बीच।

एक बात जो कह सकते कि हम सब ने स्वतः उग आये पीपल या बरगद का पेड़/ पौधा किसी न किसी दीवार, पुरानी

इमारत, पर्वत या अट्टालिकाओं पर ज़रूर देखा होगा। देखा है न?

ज़रा सोचिये पीपल या बरगद की बीज कैसे पहुंचे होंगे वहाँ तक? इनके बीज इतने हल्के भी नहीं होते के हवा उन्हें उड़ाके ले जा सके।
Ivor Cummins has been wrong (or lying) almost entirely throughout this pandemic and got paid handsomly for it.

He has been wrong (or lying) so often that it will be nearly impossible for me to track every grift, lie, deceit, manipulation he has pulled. I will use...


... other sources who have been trying to shine on light on this grifter (as I have tried to do, time and again:


Example #1: "Still not seeing Sweden signal versus Denmark really"... There it was (Images attached).
19 to 80 is an over 300% difference.

Tweet: https://t.co/36FnYnsRT9


Example #2 - "Yes, I'm comparing the Noridcs / No, you cannot compare the Nordics."

I wonder why...

Tweets: https://t.co/XLfoX4rpck / https://t.co/vjE1ctLU5x


Example #3 - "I'm only looking at what makes the data fit in my favour" a.k.a moving the goalposts.

Tweets: https://t.co/vcDpTu3qyj / https://t.co/CA3N6hC2Lq