iter jitters

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue May 4 19:56:26 EDT 2004


Diez B. Roggisch wrote:

> Steven Brent wrote:
> 
>> Btw, I just realized that a custom __getitem__ method I defined
>> earlier in the module(and unhelpfully didnt post), handles the
>> iteration in 2 lines! 
>> 
>> def __getitem__(self, index):
>>          return self.data[index]
>> 
>> This is the kind of thing I was after, because I didn't want any
>> iteration going on in the __iter__ definition itself. Trying to
>> reinvent the wheel for exercise.
> 
> Interesting. Just played around with my example and found that in
> case of absence of an __iter__-method the for - in generates keys
> from 0 to - well, I guess something like max_int, but I won't wait so
> long... - and then tries __getitem__ with that key.

This is a backwards-compatibility thing - basically there is a standard
iterator which can handle any object which supports list-like element
access (__getitem__ taking indexes from 0).

Tim Delaney




More information about the Python-list mailing list