Why is there no instancemethod builtin?

George Sakkis gsakkis at rutgers.edu
Sun Jun 19 13:52:22 EDT 2005


Michele Simionato wrote:
> I think strings do not have __iter__ on purpose, exactly to distinguish
> them from other iterables, since sometimes it is nice to consider them
> atomic, but I am not sure of this. You should ask the developers. Anyway, the
> right definition of iterable is (as I was told) "an object X such that
> iter(X) does not throw an exception".

Hmm.. not a very insightful definition unless someone knows the
implementation of iter().

> Objects following the __getitem__ protocol - such as strings - are iterables
> even if they do not have an __iter__ method.

It would be more uniform if the default 'type' metaclass added an
__iter__ method to classes that define __getitem__ but not __iter__
with something like:

from itertools import count

def __iter__(self):
    for i in count():
        try: yield self[i]
        except IndexError: raise StopIteration

George




More information about the Python-list mailing list