This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Is iterable a container type?
Type: Stage:
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, methane, rhettinger
Priority: low Keywords:

Created on 2010-11-13 19:18 by methane, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg121152 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2010-11-13 19:18
In http://docs.python.org/release/2.6.6/glossary.html, "iterable" is described as
"A container object capable of returning its members one at a time."
Is it correct? Is stream object like file a container type?

Container ABC requires only "__contains__" abstract method. I think file
is iterable but is not container.

Likewise, "and objects of any classes you define with an __iter__() or
__getitem__() method." is wrong because __getitem__ method is not relate to
iterable.
msg121166 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-13 23:51
> "iterable" is described as "A container object 
> capable of returning its members one at a time."

That wording is confusing.  I'll fix it.

> Likewise, "and objects of any classes you define 
> with an __iter__() or __getitem__() method." is 
> wrong because __getitem__ method is not relate to
> iterable

That wording is correct.  Sequences are automatically
iterable even if they don't define __iter__.  For example:

>>> class A:
...     def __getitem__(self, i):
...         if i > 10:
...             raise IndexError(i)
...         return i * 100
	
>>> list(A())
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

If you're curious, the details are in the PyObject_GetIter() function in http://svn.python.org/view/python/branches/release27-maint/Objects/abstract.c?view=markup .
msg121175 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-14 05:27
Removed the incorrect "container" reference.
See r86463.
msg121184 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2010-11-14 10:25
>> Likewise, "and objects of any classes you define
>> with an __iter__() or __getitem__() method." is
>> wrong because __getitem__ method is not relate to
>> iterable
>
> That wording is correct.  Sequences are automatically
> iterable even if they don't define __iter__.  For example:

Wow, thank you!
History
Date User Action Args
2022-04-11 14:57:08adminsetgithub: 54619
2010-11-14 10:25:23methanesetmessages: + msg121184
2010-11-14 05:27:56rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg121175
2010-11-13 23:51:22rhettingersetpriority: normal -> low

messages: + msg121166
2010-11-13 23:41:30rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
2010-11-13 19:18:27methanecreate