list element of a class with the for statement

Just van Rossum just at xs4all.nl
Tue Mar 26 09:23:54 EST 2002


In article <mailman.1017150220.26266.python-list at python.org>,
 Tjabo Kloppenburg <t.kloppenburg at billiton.de> wrote:

> or, in python 2.2 syntax:
> 
> >     def __getitem__(self,i):
> >         return self.__records[i]
> 
>   def __iter__(self):
>     self.ptr = 0
>     return self
> 
>   def next(self):
>      if self.ptr < (len(self.__records) - 1):
>        self.ptr = self.ptr + 1
>        return self.__records[self.ptr]
>      else:
>        raise StopIteration

But that's different! In the "old-style" iterator the next bit will work 
as expected:

  for entry1 in db:
      for entry2 in db:
          ...

But with the way you wrote the new-style iterator it won't. It's also 
not thread-safe.

Just



More information about the Python-list mailing list