"George Sakkis" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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
Unfortunately it doesn't work: getitem can be defined for a class that acts like a dictionary: that is, the items are not integers, let alone integers that extend in a strict sequence from 0. John Roth > > George > -- http://mail.python.org/mailman/listinfo/python-list