Re: Why can’t for-of be applied to iterators?

Axel Rauschmayer axel at
Tue Jun 11 02:08:29 PDT 2013

> The idea that iterators return themselves from their @@iterator (__iter__) hook has years of actual programmer mileage in Python. I take that experience seriously, compared to arguments from purity that tax the common use-cases.

I’d be happy with either solution, but let’s compare:

AFAICT, the tax is on for-of (and Array.from()): they need to additionally check whether an object is an iterator.

On the other hand, turning every iterator into an iterable puts the burden on people implementing iterators: they have to implement the iterable interface. And it becomes harder to distinguish the interfaces, should you need to [1]. IMO, the purity argument also counts: this technique implies that iterators are a subset of iterables, but that isn’t true: For iterables, each invocation of `iterator` creates a new object, for iterators, it doesn’t.

As an aside, Python 3 uses a special name for the `next` method (it previously didn’t). Maybe ES6 should use a symbol here, too. That would eliminate the concerns about false positives, should for-of ever work with iterators.


Dr. Axel Rauschmayer
axel at


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

More information about the es-discuss mailing list