[Python-Dev] metaclass insanity (original) (raw)
Just van Rossum just@letterror.com
Tue, 5 Nov 2002 16:08:51 +0100
- Previous message: [Python-Dev] metaclass insanity
- Next message: [Python-Dev] metaclass insanity
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[JvR]
> class Outer: > [snip snip] > def iter(self): > while not self.isAtEnd(): > result = self.outer.currentState() > self.outer.advanceState() > yield result
[David Abrahams]
This only works for 'self-iterable' types like files, right?
No, the generator-iterator can hold state in the form of local variables. That isn't used in this example, though.
[later]
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?
Heh, yeah, that's what generators do. You call a generator-function (like the iter() method above) and it returns a generator-iterator. The magic word is "yield".
Just
- Previous message: [Python-Dev] metaclass insanity
- Next message: [Python-Dev] metaclass insanity
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]