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/029904.html below:

[Python-Dev] metaclass insanity

[Python-Dev] metaclass insanity [Python-Dev] metaclass insanityDavid 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




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