[Python-ideas] Introduce collections.Reiterable
Tim Delaney
timothy.c.delaney at gmail.com
Sat Sep 21 00:00:30 CEST 2013
On 20 September 2013 21:10, Steven D'Aprano <steve at pearwood.info> wrote:
>
> py> class Seq:
> ... def __getitem__(self, index):
> ... if 0 <= index < 5: return index+1000
> ... raise IndexError
> ...
> py> s = Seq()
> py> isinstance(s, Iterable) # The ABC claims Seq is not iterable.
> False
> py> for x in s: # But it actually is.
> ... print(x)
> ...
> 1000
> 1001
> 1002
> 1003
> 1004
>
>
> Can anyone convince me this is not a bug in the Iterable ABC?
>
I think there is a distinction here between collections.Iterable (as a
defined ABC) and something that is "iterable" (lowercase "i"). As you've
noted, an "iterable" is "An object capable of returning its members one at
a time".
So I think a valid definition of reiterable (barring pathological cases) is:
obj is not iter(obj)
(assuming of course that obj is iterable).
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64
bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Seq:
... def __getitem__(self, index):
... if 0 <= index < 5:
... return index+1000
... raise IndexError
...
>>> s = Seq()
>>> s is iter(s)
False
>>> i = iter(s)
>>> i is iter(i)
True
>>> t = ()
>>> t is iter(t)
False
>>> i = iter(t)
>>> i is iter(i)
True
>>>
Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130921/70711ae6/attachment-0001.html>
More information about the Python-ideas
mailing list