[issue18558] Iterable glossary entry needs clarification

R. David Murray report at bugs.python.org
Fri Jul 26 03:25:40 CEST 2013


R. David Murray added the comment:

The definition of an Iterable is a class that defines an __iter__  method.  Your class does not, so the behavior you site is correct.

The glossary entry for 'iterable' could use a little clarification.  A class that defines __getitem__ is an iterable if and only if it returns results when passed integers.  Since the documentation for Iterable references that glossary entry, it should probably also be explicit that defining __getitem__ does not (because of the forgoing limitation) cause isinstance(x, Iterable) to be True.  For a class that does not define __iter__, you must explicitly register it with Iterable.

To see why this must be so, consider this:

  >>> y = IsIterable({'a': 'b', 'c': 'd'})
  >>> [x for x in y]
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 1, in <listcomp>
    File "<stdin>", line 5, in __getitem__
  KeyError: 0

----------
assignee:  -> docs at python
components: +Documentation -Library (Lib)
nosy: +docs at python, r.david.murray
stage:  -> needs patch
title: Iterables not detected correctly -> Iterable glossary entry needs clarification
versions: +Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18558>
_______________________________________


More information about the Python-bugs-list mailing list