Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > iterable terminology (for language lawyers)

Reply
Thread Tools

iterable terminology (for language lawyers)

 
 
Michele Simionato
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
 
 
 
Leif K-Brooks
Guest
Posts: n/a
 
      03-16-2005
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().
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      03-16-2005
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: well-defined terminology versus generally accepted terminology regarding pointers and arrays Paul C++ 63 04-24-2011 12:42 PM
jdk 1.5 enhanced for loop, Iterable and arrays Remi Bastide Java 5 03-29-2010 11:28 AM
filter iterable based on predicate take from another iterable bernhard.voigt@gmail.com Python 2 12-10-2008 10:44 AM
Whay aren't Strings Iterable? Googmeister Java 13 10-10-2005 03:51 AM
Word for a non-iterator iterable? Leif K-Brooks Python 5 02-07-2005 09:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57