[Python-ideas] Suggested MapView object (Re: __len__() for map())

Steven D'Aprano steve at pearwood.info
Thu Dec 13 00:11:22 EST 2018


On Wed, Dec 12, 2018 at 08:06:17PM -0800, Chris Barker - NOAA Federal wrote:
> >>> and the test for an iterator is:
> >>>
> >>>   obj is iter(obj)
> 
> Is that a hard and fast rule? 

Yes, that's the rule for the iterator protocol.

Any object can have an __iter__ method which returns anything you want. 
(It doesn't even have to be iterable, this is Python, and if you want to 
shoot yourself in the foot, you can.)

But to be an iterator, the rule is that obj.__iter__() must return obj 
itself. Otherwise we say that obj is an iterable, not an iterator.

https://docs.python.org/3/library/stdtypes.html#iterator.__iter__


-- 
Steve


More information about the Python-ideas mailing list