[Zope3-dev] Number of languages in Zope 3

Guido van Rossum guido@python.org
Mon, 14 Apr 2003 07:39:09 -0400


> Guido van Rossum wrote:
> > This particular example can be rewritten as a list comprehension:
> > 
> >   def getSnippetInfo(self):
> >       return [{'title': snippet.headline(version='Brief'),
> >                'tease': snippet.teaseText(maxWords=100)}
> >                for snippet in self.context.values()
> >                if ISnippet.isImplementedBy(x)]
> > 
> > which also avoids the dual looping thwt Chris asks about.

[Chris]
> How does it do that?

I *think* you were referring to the fact that there are two loops in
Tres's original:

> >>    def getSnippetInfo(self):
> >>        results = []
> >>        for snippet in [x for x in self.context.values()
> >>                          if ISnippet.isImplementedBy(x) ]:
> >>            results.append({'title' : snppet.headline(version='Brief'),
> >>                            'tease' : snippet.teaseText(maxWords=100)})
> >>        return results

Since the list comprehension contains only one loop, it's pretty
obvious that it removes the double loop. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)