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: Docs don't define sequence-ness very well
Type: enhancement Stage:
Components: Documentation Versions: Python 2.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, facundobatista, pitrou, skip.montanaro, skip.montanaro
Priority: normal Keywords:

Created on 2003-01-31 23:52 by skip.montanaro, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Messages (5)
msg60304 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2003-01-31 23:52
The notion of what a sequence is seems to be
somewhat ill-defined.  If I execute

    for k in None: pass

a TypeError is raised with "iteration over
non-sequence" as the message.  I searched the
source for that message and found three
occurrences, one each in
Objects/{abstract,classobject,typeobject}.c.
In abstract.c a non-sequence is an object
which fails the PySequence_Check(s) test.  In
typeobject.c:slot_tp_iter, a sequence is
defined to have a __getitem__ method.  In
classobject.c a sequence must have either
__iter__ or __getitem__ and if __iter__ is
defined, it must return something which
passes PyIter_Check().

So, if I want to do a sniff test for a
sequence is it "close enough" to execute

    (hasattr(s, "__getitem__") or
     hasattr(s, "__iter__"))

or should I execute

    iter(s)

?  Since this seems to be a fairly nebulous
concept it makes sense to me that it should
be defined somewhere.  I checked the ref
manual (types.html) in the Sequences section
but saw nothing mentioned.  If this is
already defined somewhere I'd be happy to be
pointed in the right direction.

I would just update types.html if I knew what
the answer was...
msg68584 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-06-22 21:58
Skip, don't you think it's better to raise this kind of generic question
in the python-dev list?

This should probably lay down here for ever before a discussion raises
to decide this.
msg68727 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-06-25 13:27
What is the bug actually? "for k in s" is defined to work on any
iterable, not only on sequences. And "iterable" is clearly defined, it's
sufficient to check whether s.__iter__ exists or whether iter(s) succeeds...
msg69089 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-07-02 13:25
(Sorry for the delay responding.  Gmail thought Facundo's response was
spam. :-/)  In defense of my bug report, note that I submitted it in
January 2003!  It's quite possible that the docs have improved in this
regard since then.

If you think the docs are up-to-date in this regard now (I have no time
to do a careful investigation) please close the ticket.

Skip
msg69093 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-07-02 14:31
I think the glossary now has some good definitions of iterable and
sequence, so we can close this. (after 5 years!)
History
Date User Action Args
2022-04-10 16:06:25adminsetgithub: 37888
2008-07-02 14:31:10benjamin.petersonsetstatus: open -> closed
resolution: out of date
messages: + msg69093
nosy: + benjamin.peterson
2008-07-02 13:25:48skip.montanarosetnosy: + skip.montanaro
messages: + msg69089
2008-06-25 13:27:50pitrousetnosy: + pitrou
messages: + msg68727
2008-06-22 21:58:16facundobatistasetnosy: + facundobatista
messages: + msg68584
2008-05-11 23:18:46benjamin.petersonsettype: enhancement
2003-01-31 23:52:37montanaro.historiccreate