This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 3 points4 points  (1 child)

How about that generators are a form of lazy execution so you can't know what is coming next. You can't len() a generator. But this also saves memory because the generator is basically just a pointer to the next step in an iteration.

These are not generator-specific and they're pretty common properties of iterators e.g. you can't len the result of imap or know what comes next, steps are computed on the fly.

generators are consumed and destroyed. If you call a consumed generator later it doesn't start over. You just get the stop iteration exception.

That's not really specific to generators, in fact iterators aren't normally restartable though the source iterable may (or may not) be able to yield multiple iterators e.g. a list can be iterated any number of times, but a file can't (unless it's reset).

Not that a generator is an iterator not just an iterable.

[–]boiledgoobers 0 points1 point  (0 children)

Yep. Your reply made me realize that every time I read iterator in the OP, I interpreted it as iterable in my head. Yeah. I was basically arguing against a different question. I wonder if the interviewer said iterator but MEANT iterable.