stranac wrote:Generators are a very useful tool and an important language concept, knowing more about them can't hurt.
I'd recommend this as an introduction: http://nedbatchelder.com/text/iter.html
The text starts out with improving some common complications from other languages like the following, before going on to more advanced examples of uses of generators, iterators, etc. Simple descriptions and thoughtful suggestions.
- Code: Select all
for i in range(len(x)):
return x(i) # complicated, locates elements by index
for e in x:
return e # easy access to every element