[Python-ideas] iterable: next() and __iter__() -- and __reset()

Masklinn masklinn at masklinn.net
Thu Mar 4 12:07:48 CET 2010

On 4 Mar 2010, at 11:35 , spir wrote:
> (3) What I miss actually for iterables (which are their own iterator) is a kind of __reset__(). In some cases, it is only needed to allow a new iteration from start. But it may even be needed to set some startup data the first time. __reset__() would thus be called once before the first call to next(). (Sure, "reset" may not be the best term. Maybe "begin" or "startup"? The sense is: "Prepare to yield the first item!")

The use case you propose (and demonstrate in your example) shows that you're creating an iterating view over your container. Which doesn't seem the role of __iter__ at all as far as I understand, so you should indeed use either __call__ or a separate method call. Slice, for instance. In fact, in your case I think supporting slicing/islicing would make far more sense than the solution you've elected):

    tripleCubes = []
    cubes = Powers(3)
    for sq in cubes[6, 17]:
        if sq%3 == 0:

You could also compose your iterable with existing tools such as those in ``itertools``, or create your own composable iterable transformers/manipulators (I strongly recommend David M. Beazley's presentations on generators for that [1][2].

With those, removing __call__ from your Power[3] class and setting `self.n = 0` (and `self.max = None`) in the constructor:

    cubes = Powers(3)
    tripleCubes = filter(lambda sq: sq%3 == 0, (islice(cubes, 6, 17)))

Or (to avoid the pretty ugly lambda and use a listcomp):

    cubes = Powers(3)
    tripleCubes = [sq for sq in islice(cubes, 6, 16)
                      if sq%3 == 0]

[1] Generator Tricks for Systems Programmers http://www.dabeaz.com/generators/
[2] A Curious Course on Coroutines and Concurrency http://www.dabeaz.com/coroutines/
[3] I hope and believe you wouldn't actually write such code outside of an example as there are much better ways to achieve the same thing in Python

More information about the Python-ideas mailing list