Peter Hansen wrote: > Thomas Lotze wrote: >> I can see two possibilities to do this: either the current file position >> has to be read from somewhere (say, a mutable object passed to the >> generator) after each yield, [...] > > The third approach, which is certain to be cleanest for this situation, is > to have a custom class which stores the state information you need, and > have the generator simply be a method in that class.
Which is, as far as the generator code is concerned, basically the same as passing a mutable object to a (possibly standalone) generator. The object will likely be called self, and the value is stored in an attribute of it. Probably this is indeed the best way as it doesn't require the programmer to remember any side-effects. It does, however, require a lot of attribute access, which does cost some cycles. A related problem is skipping whitespace. Sometimes you don't care about whitespace tokens, sometimes you do. Using generators, you can either set a state variable, say on the object the generator is an attribute of, before each call that requires a deviation from the default, or you can have a second generator for filtering the output of the first. Again, both solutions are ugly (the second more so than the first). One uses side-effects instead of passing parameters, which is what one really wants, while the other is dumb and slow (filtering can be done without taking a second look at things). All of this makes me wonder whether more elaborate generator semantics (maybe even allowing for passing arguments in the next() call) would not be useful. And yes, I have read the recent postings on PEP 343 - sigh. -- Thomas -- http://mail.python.org/mailman/listinfo/python-list