instantiating generators

Axel Rauschmayer axel at
Fri Mar 15 20:56:17 PDT 2013

> On Fri, Mar 15, 2013 at 11:22 AM, Axel Rauschmayer <axel at> wrote:
> I would prefer having to use `new` (but don’t have strong feelings about it): generators are more like constructors than like functions. When I first started experimenting with them in Firefox, it took me a while to figure that out (even though it is obvious in hindsight). With `new`, I’d probably have figured it out quicker.
> I agree with Tab: I wonder if your mental model for generators might not continue to evolve.
> I would hate to have to write:
>     for (k of new tree.preorder())
> rather than:
>     for (k of tree.preorder())

Yes, it will probably evolve. The wish for `new` comes up if you know what’s inside a generator function and are surprised that you need to invoke a method to start executing the function body. If you treat it as a black box then `new` makes less sense, then it is roughly a function or method that returns an iterator.

(Tongue in cheek, you could argue that Python does treat generators like classes)

Dr. Axel Rauschmayer
axel at


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list