instantiating generators

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


> On Fri, Mar 15, 2013 at 11:22 AM, Axel Rauschmayer <axel at rauschma.de> 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 rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130316/947d181f/attachment.html>


More information about the es-discuss mailing list