Understanding generators in Python

I am reading the Python cookbook at the moment and am currently looking at generators. I’m finding it hard to get my head round.

As I come from a Java background, is there a Java equivalent? The book was speaking about ‘Producer / Consumer’, however when I hear that I think of threading.

What is a generator and why would you use it? Without quoting any books, obviously (unless you can find a decent, simplistic answer direct from a book). Perhaps with examples, if you’re feeling generous!

13 Answers
13

Leave a Comment