iterable terminology (for language lawyers)

Discussion in 'Python' started by Michele Simionato, Mar 16, 2005.

  1. 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
     
    Michele Simionato, Mar 16, 2005
    #1
    1. Advertising

  2. 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().
     
    Leif K-Brooks, Mar 16, 2005
    #2
    1. Advertising

  3. 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
     
    Raymond Hettinger, Mar 16, 2005
    #3
  4. 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
     
    Michele Simionato, Mar 16, 2005
    #4
  5. [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
     
    Raymond Hettinger, Mar 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    Nested Class Language Lawyers

    Roedy Green, Aug 24, 2005, in forum: Java
    Replies:
    9
    Views:
    364
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. Evan
    Replies:
    1
    Views:
    318
    Kai-Uwe Bux
    Dec 5, 2006
  3. Kenny McCormack

    One for the language lawyers

    Kenny McCormack, Jun 9, 2008, in forum: C Programming
    Replies:
    11
    Views:
    465
    Serve Lau
    Jun 10, 2008
  4. Replies:
    2
    Views:
    386
    James Stroud
    Dec 10, 2008
  5. Paul
    Replies:
    63
    Views:
    1,317
Loading...

Share This Page