[Python-ideas] Introduce collections.Reiterable
Neil Girdhar
mistersheik at gmail.com
Mon Sep 23 02:24:05 CEST 2013
Why not just add one line?
def __iter__(self): return (self.__getitem__(i) for i in itertools.count())
On Sun, Sep 22, 2013 at 7:46 PM, Steven D'Aprano <steve at pearwood.info>wrote:
> On Sun, Sep 22, 2013 at 12:37:52PM -0400, Terry Reedy wrote:
> > On 9/22/2013 10:22 AM, Nick Coghlan wrote:
> >
> > >The __getitem__ fallback is a backwards
> > >compatibility hack, not part of the formal definition of an iterable.
> >
> > When I suggested that, by suggesting that the fallback *perhaps* could
> > be called 'semi-deprecated, but kept for back compatibility' in the
> > glossary entry, Raymond screamed at me and accused me of trying to
> > change the language. He considers it an intended language feature that
> > one can write a sequence class and not bother with __iter__. I guess we
> > do not all agree ;-).
>
> Raymond did not "scream", he wrote *one* word in uppercase for emphasis.
> I quote:
>
> It is NOT deprecated. People use and rely on this behavior. It is
> a guaranteed behavior. Please don't use the glossary as a place to
> introduce changes to the language.
>
>
> I agree, and I disagree with Nick's characterization of the sequence
> protocol as a "backwards-compatibility hack". It is an elegant protocol
> for implementing iteration of sequences, an old and venerable one that
> predates iterators, and just as much of Python's defined iterable
> behaviour as the business with calling next with no argument until it
> raises StopIteration. If it were considered *merely* for backward
> compatibility with Python 1.5 code, there was plenty of opportunity to
> drop it when Python 3 came out.
>
> The sequence protocol allows one to write a lazily generated,
> potentially infinite sequence that still allows random access to items.
> Here's a toy example:
>
>
> py> class Squares:
> ... def __getitem__(self, index):
> ... return index**2
> ...
> py> for sq in Squares():
> ... if sq > 9: break
> ... print(sq)
> ...
> 0
> 1
> 4
> 9
>
>
> Because it's infinite, there's no value that __len__ can return, and no
> need for a __len__. Because it supports random access to items, writing
> this as an iterator with __next__ is inappropriate. Writing *both* is
> unnecessary, and complicates the class for no benefit. As written,
> Squares is naturally thread-safe -- two threads can iterate over the
> same Squares object without interfering.
>
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/OumiLGDwRWA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130922/dae51eb0/attachment-0001.html>
More information about the Python-ideas
mailing list