[Python-Dev] metaclass insanity (original) (raw)

David Abrahams dave@boost-consulting.com
05 Nov 2002 09:45:30 -0500


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

Alex Martelli wrote:

> class Outer: > [snip snip] > def iter(self): > class Inner: > def init(self, outer): > self.outer = outer > def iter(self): > return self > def next(self): > if self.outer.isAtEnd(): > raise StopIteration > else: > result = self.outer.currentState() > self.outer.advanceState() > return result > return Inner(self) While I'm sure this was just a theoretical example, this specific case is much easier written as a generator (and I love the idiom of making the iter method a generator, I think it's underused...). class Outer: [snip snip] def iter(self): while not self.isAtEnd(): result = self.outer.currentState() self.outer.advanceState() yield result

This only works for 'self-iterable' types like files, right?

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