4 ms·
First, because Python's iterators are a way of forming a wrapper around an arbitrary data set. This is in contrast to (say) C++ iterators, which are merely poin
by ggchappell 15y ago
First, because Python's iterators are a way of forming a wrapper around an arbitrary data set. This is in contrast to (say) C++ iterators, which are merely pointers into a data set, and so it takes two of them to specify a range of data.
Second, Python's "for" construction, along with imap and other such things, mean that we can program with iterators, without treating them as mutable objects; they merely wrap the data returned by some computation.
Third, Python's iterators have no write capability. Again, contrast this with C++, where output iterators are the usual way to answer the question, "What do I do with this data?"
Add Python's "yield" to the mix, and the result is a very natural, efficient way for a function that computes a large data set, to return the entire set (functional style), rather than writing it somewhere (very non-functional).
- wahnfrieden 15y agoThanks for elaborating, that makes sense. It wasn't apparent to me, but those are good points.