A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2002-November/029908.html below:

[Python-Dev] metaclass insanity

[Python-Dev] metaclass insanity [Python-Dev] metaclass insanityDavid Abrahams dave@boost-consulting.com
05 Nov 2002 09:58:21 -0500
Just van Rossum <just@letterror.com> writes:

> [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.

Ahh, closures...

> [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".

Too cool!

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




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4