By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

iterable terminology (for language lawyers)

P: n/a
According to the standand library
(http://docs.python.org/lib/typeiter.html)
an *iterable* is something with an __iter__ method. This means that
strings
are *not* iterable. However I can loop over a string without problem
and I would
say that an iterable is anything I can iterate over. Of course I am
*not* proposing
we add __iter__ to strings (it is convenient for me to be able to
distinguish
strings from other "iterables", since more often than not a string
wants to
be treated as an atomic object). The reason why I ask for a
clarification is
that I am going to give a course at the ACCU conference, so I want to
make
sure I use the rigth terminology.

Michele Simionato

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Michele Simionato wrote:
According to the standand library
(http://docs.python.org/lib/typeiter.html) an *iterable* is something
with an __iter__ method. This means that strings are *not* iterable.


In general, the definitions people in the Python community tend to use are:

Iterable: An object which doesn't raise TypeError when passed to iter().
Reiterable: An object which can be passed to iter() multiple times
without returning itself.
Iterator: An object which defines a next() method and returns itself
when passed to iter().
Jul 18 '05 #2

P: n/a
Michele Simionato]
According to the standand library
(http://docs.python.org/lib/typeiter.html)
an *iterable* is something with an __iter__ method. This means that
strings are *not* iterable.
The referenced section also says, "Sequences, described below in more
detail, always support the iteration methods." And since strings are
sequences, strings
*are* iterable.
The reason why I ask for a clarification is that I am going to
give a course at the ACCU conference, so I want to
make sure I use the rigth terminology.


You're best bet is to quote the tutorial's glossary,
http://docs.python.org/tut/node18.html :

iterable
A container object capable of returning its members one at a time.
Examples of iterables include all sequence types (such as list, str,
and tuple) and some non-sequence types like dict and file and objects
of any classes you define with an __iter__() or __getitem__() method.
Iterables can be used in a for loop and in many other places where a
sequence is needed (zip(), map(), ...). When an iterable object is
passed as an argument to the builtin function iter(), it returns an
iterator for the object. This iterator is good for one pass over the
set of values. When using iterables, it is usually not necessary to
call iter() or deal with iterator objects yourself. The for statement
does that automatically for you, creating a temporary unnamed variable
to hold the iterator for the duration of the loop. See also iterator,
sequence, and generator.
Raymond Hettinger

Jul 18 '05 #3

P: n/a
R. Hettinger wrote:
You're best bet is to quote the tutorial's glossary,
http://docs.python.org/tut/node18.html :


Aha! That glossary looks like a nice new addition to the tutorial.
Maybe the standard library and the language
reference should link to it somewhere? (maybe
there already such links but I missed them).

Would you agree with Leif's definition that iterable is
any x such that iter(x) does not raise an error? On top
of my head I don't find any counter example and it
is the kind of definition I had in mind.

Michele Simionato

Jul 18 '05 #4

P: n/a
[R. Hettinger]
You're best bet is to quote the tutorial's glossary,
http://docs.python.org/tut/node18.html :

[Michele Simionato] Aha! That glossary looks like a nice new addition to the tutorial.
Maybe the standard library and the language
reference should link to it somewhere? (maybe
there already such links but I missed them).
You know where to submit a patch ;-)

Would you agree with Leif's definition that iterable is
any x such that iter(x) does not raise an error?


Sure, that is an accurate but not especially informative tautology.

A person can memorize that definition and still know nothing useful like what
they do (return their elements one at a time), what they look like (__iter__ or
__getitem__ methods), or how they work.

You might also mention that monglable is defined as any x such that mongle(x)
does not fail. Knowing that fact is the key to a complete and deep
understanding of monglation ;-)

Raymond Hettinger
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.