Iterator & Generator GotchasPython iterators are objects (or functions, using some magic) that repeatedly produce values, one at a time, until they get exhausted. Python introduced this general feature to efficiently support things like:
Without iterators, Generators are functions that magically create iterators instead of just returning values (ignoring some technicalities). Generators are the most common gateway to iterators, and are thus the more commonly used term for the whole area. When iterators were introduced, a number of standard things that had previously returned lists started returning iterators, and using a generator instead of just returning a list became part of the common Python programming idioms. In many cases it can be tempting, and temptingly easy, to replace things that return lists with generators; it looks like it should just work, and it mostly does. It can be similarly tempting to just ignore the difference in the standard Python modules. But there are some gotchas when you write code like this, and I have the stubbed toes to prove it. At one point or another, I've made all of these iterator-confusion mistakes in my code. Iterators are always truet = generate_list(some, inputs) if not t: return print "Header Line:" for item in t: ..... If There's really no way to see if an iterator contains anything except to try to get a value from it. And there's no 'push value back onto iterator' operation. Iterators can't be saved
If (Technically I believe there are semi-magical ways to copy iterators. I suspect one is best off avoiding them unless you really have to save an iterator copy.) I can't use list methods on iteratorst = generate_list(some, inputs) t.sort() t = t[:firstN] # ... admire the pretty explosions Of course, iterators don't have general list functions like t = list(generate_list(some, inputs)) t.sort(); t = t[:firstN] Fortunately, Writing recursive generatorsSometimes the most natural structure for a generator is a recursive one. This works, but you have to bear in mind a twist: you cannot simply return the results of the recursive calls. This is because the recursive results are themselves iterators, and if you return them straight your callers get iterators that produce a stream of iterators that produce a stream of iterators that someday, at some level, produce actual results. (But by that time the caller has given up in despair.) Instead each time you recurse, you have to expand the resulting iterator and return each result, like so:
This implies that significantly recursive generators can be quite inefficient, as they will spend a great deal of time trickling results up through all the levels involved. |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |