[Python-ideas] Introduce collections.Reiterable
g.brandl at gmx.net
Sun Sep 22 10:58:40 CEST 2013
On 09/22/2013 06:56 AM, Nick Coghlan wrote:
> On 20 Sep 2013 19:49, "Steven D'Aprano" <steve at pearwood.info> wrote:
>> On Thu, Sep 19, 2013 at 11:02:57PM +1000, Nick Coghlan wrote:
>> > On 19 September 2013 22:18, Steven D'Aprano <steve at pearwood.info> wrote:
>> > > At the moment, dict views aren't directly iterable (you can't call
>> > > next() on them). But in principle they could have been designed as
>> > > re-iterable iterators.
>> > That's not what iterable means. The iterable/iterator distinction is
>> > well defined and reflected in the collections ABCs:
>> Actually, I think the collections ABC gets it wrong, according to both
>> common practice and the definition given in the glossary:
>> More on this below.
>> As for my comment above, dict views don't obey the iterator protocol
>> themselves, as they have no __next__ method, nor do they obey the
>> sequence protocol, as they are not indexable. Hence they are not
>> *directly* iterable, but they are *indirectly* iterable, since they have
>> an __iter__ method which returns an iterator.
> Um, no. Everywhere Python iterates over anything, we call iter(obj)
> first. If there is anywhere we don't do that, it's a bug.
>> I don't think this is a critical distinction. I think it is fine to call
>> views "iterable", since they can be iterated over. On the rare occasion
>> that it matters, we can just do what I did above, and talk about objects
>> which are directly iterable (e.g. iterators, sequences, generator
>> objects) and those which are indirectly iterable (e.g. dict views).
> Or you could just use the existing terminology and talk about
> iterables vs iterators instead of inventing your own terms.
Ack. Please don't create new terms, rather suggest an improvement to the
glossary definition if you think it's inadequate.
More information about the Python-ideas