What is a 𝗴𝗲𝗻𝗲𝗿𝗮𝘁𝗼𝗿 in Python? This is one of the most asked questions in interviews, and it’s understandable why. A generator is a concept introduced in Python as an alternative to arrays, mainly to save memory usage. It’s what allows a function to produce values lazily, one at a time. In most assumptions, the keyword yield belongs to generators, and it is what makes them what they are. But that’s not entirely correct. A generator is a subclass of an iterator, and any iterator can be considered iterable. While all generators are iterators, not all iterators are generators. What makes a generator special compared to a regular iterator is its 𝘀𝗲𝗻𝗱, 𝘁𝗵𝗿𝗼𝘄 and 𝗰𝗹𝗼𝘀𝗲 methods. Now you’ve learned about Python generators in a true sense! The image below depicts client/server analogy build over generator. #Programming #Python #Iterators #Generator #Blasquared #Rarblack 𝘋𝘪𝘴𝘤𝘭𝘢𝘪𝘮𝘦𝘳: 𝘛𝘩𝘪𝘴 𝘱𝘰𝘴𝘵 𝘸𝘢𝘴 𝘸𝘳𝘪𝘵𝘵𝘦𝘯 𝘸𝘪𝘵𝘩𝘰𝘶𝘵 𝘳𝘦𝘭𝘪𝘢𝘯𝘤𝘦 𝘰𝘯 𝘈𝘐.
You are true. One aspect is what you said. The other aspect, bigger picture I can say, is cooperative concurrency paradigm implemented by the generators.
Next interview question: what is the difference between a generator and an async generator, as both relies on coroutines?