🤔 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

Starting a new project using #Angular? Here is a list of all the stuff i use to launch my projects the fastest i can.

A THREAD 👇

Have you heard about Monorepo? I created one with all my Angular (and Nest) projects using
https://t.co/aY5llDtXg8.

I can share A LOT of code with it. Ex: Everytime i start a new project, i just need to import an Auth lib, that i created, and all Auth related stuff is set up.

Everyone in the Angular community knows about https://t.co/kDnunQZnxE. It's not the most beautiful component library out there, but it's good and easy to work with.

There's a bunch of state management solutions for Angular, but https://t.co/RJwpn74Qev is by far my favorite.

There's a lot of boilerplate, but you can solve this with the built-in schematics and/or with your own schematics

Are you not using custom schematics yet? Take a look at this:

https://t.co/iLrIaHVafm
https://t.co/3382Tn2k7C

You can automate all the boilerplate with hundreds of files associates with creating a new feature.

You May Also Like

दधीचि ऋषि को मनाही थी कि वह अश्विनी कुमारों को किसी भी अवस्था में ब्रह्मविद्या का उपदेश नहीं दें। ये आदेश देवराज इन्द्र का था।वह नहीं चाहते थे कि उनके सिंहासन को प्रत्यक्ष या परोक्ष रुप से कोई भी खतरा हो।मगर जब अश्विनी कुमारों ने सहृदय प्रार्थना की तो महर्षि सहर्ष मान गए।


और उन्होनें ब्रह्मविद्या का ज्ञान अश्विनि कुमारों को दे दिया। गुप्तचरों के माध्यम से जब खबर इन्द्रदेव तक पहुंची तो वे क्रोध में खड़ग ले कर गए और महर्षि दधीचि का सर धड़ से अलग कर दिया।मगर अश्विनी कुमार भी कहां चुप बैठने वाले थे।उन्होने तुरंत एक अश्व का सिर महर्षि के धड़ पे...


...प्रत्यारोपित कर उन्हें जीवित रख लिया।उस दिन के पश्चात महर्षि दधीचि अश्वशिरा भी कहलाए जाने लगे।अब आगे सुनिये की किस प्रकार महर्षि दधीचि का सर काटने वाले इन्द्र कैसे अपनी रक्षा हेतु उनके आगे गिड़गिड़ाए ।

एक बार देवराज इन्द्र अपनी सभा में बैठे थे, तो उन्हे खुद पर अभिमान हो आया।


वे सोचने लगे कि हम तीनों लोकों के स्वामी हैं। ब्राह्मण हमें यज्ञ में आहुति देते हैं और हमारी उपासना करते हैं। फिर हम सामान्य ब्राह्मण बृहस्पति से क्यों डरते हैं ?उनके आने पर क्यों खड़े हो जाते हैं?वे तो हमारी जीविका से पलते हैं। देवर्षि बृहस्पति देवताओं के गुरु थे।

अभिमान के कारण ऋषि बृहस्पति के पधारने पर न तो इन्द्र ही खड़े हुए और न ही अन्य देवों को खड़े होने दिया।देवगुरु बृहस्पति इन्द्र का ये कठोर दुर्व्यवहार देख कर चुप चाप वहां से लौट गए।कुछ देर पश्चात जब देवराज का मद उतरा तो उन्हे अपनी गलती का एहसास हुआ।