[Python-Dev] metaclass insanity

David Abrahams dave@boost-consulting.com
05 Nov 2002 09:47:50 -0500


Just van Rossum <just@letterror.com> writes:

> Just van Rossum wrote:
> 
> > class Outer:
> >     [snip snip]
> >     def __iter__(self):
> >         while not self.isAtEnd():
> >             result = self.outer.currentState()
> >             self.outer.advanceState()
> >             yield result
> 
> PS, getting more off-topic, I'm somewhat surprised that the above is more
> compact and readable than the obvious generator-less equivalent:
> 
> class Outer:
>     [snip snip]
>     def __iter__(self):
>         return self
>     def next(self):
>         if self.isAtEnd():
>             raise StopIteration
>         else:
>             result = self.outer.currentState()
>             self.outer.advanceState()
>             return result

Hey, wait a sec!

It doesn't surprise me at all that the above is more compact. I don't
see any next() interface, nor any raising of StopIteration. Does it
really satisfy the iterator protocol?

-- 
                    David Abrahams
dave@boost-consulting.com * http://www.boost-consulting.com