[Python-ideas] discontinue iterable strings

Nick Coghlan ncoghlan at gmail.com
Sun Aug 21 02:16:48 EDT 2016


On 21 August 2016 at 16:02, eryk sun <eryksun at gmail.com> wrote:
> On Sun, Aug 21, 2016 at 5:27 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> Hmm. It would somehow need to be recognized as "not iterable". I'm not
>> sure how this detection is done; is it based on the presence/absence
>> of __iter__, or is it by calling that method and seeing what comes
>> back? If the latter, then sure, an __iter__ that raises would cover
>> that.
>
> PyObject_GetIter calls __iter__ (i.e. tp_iter) if it's defined. To get
> a TypeError, __iter__ can return an object that's not an iterator,
> i.e. an object that doesn't have a __next__ method (i.e. tp_iternext).

I believe Chris's concern was that "isintance(obj,
collections.abc.Iterable)" would still return True.

That's actually a valid concern, but Python 3.6 generalises the
previously __hash__ specific "__hash__ = None" anti-registration
mechanism to other protocols, including __iter__:
https://hg.python.org/cpython/rev/72b9f195569c

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list