Why is there no instancemethod builtin?

John Roth newsgroups at jhrothjr.com
Sun Jun 19 15:34:13 EDT 2005


"George Sakkis" <gsakkis at rutgers.edu> wrote in message 
news:1119203542.789312.5620 at g44g2000cwa.googlegroups.com...
> 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
> 




More information about the Python-list mailing list