In Python, what is the difference between a generator and a generator function?
💡 Model Answer
A generator function is a normal function that contains one or more yield statements. When you call it, it does not execute the body immediately; instead, it returns a generator object. The generator object is an iterator that produces values lazily: each time you call next() on it, the function runs until it hits the next yield, returns that value, and pauses its state. In short, the generator function is the definition that creates the generator, while the generator is the runtime object that yields values on demand.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500