iterable terminology (for language lawyers)

M

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. 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
 
L

Leif K-Brooks

Michele said:
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().
 
R

Raymond Hettinger

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
 
M

Michele Simionato

R. Hettinger said:
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
 
R

Raymond Hettinger

[R. Hettinger]
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top