Proposed new collection methods

Jeff Schwab jeffrey.schwab at
Sun Aug 7 06:50:09 CEST 2005

Robert Kern wrote:

> (s for s in iter(self) is test(s)) is a generator expression. It is 
> roughly equivalent to
>   def g(self, test=lambda x: True):
>     for s in iter(self):
>       if test(s):
>         yield s
> Now, if I were to do
>   item = g(self, test).next()
> the generator would execute the code until it reached the yield 
> statement which happens when it finds the first item that passes the 
> test. That item will get returned, and execution does not return to the 
> generator again.
> That implementation does indeed return as soon as it locates the first 
> item, so yes, I was being retarded.

Wow, that's cool!  Very reminiscent of C++ input iterators, but a lot 
cleaner and shorter.  Thanks.

More information about the Python-list mailing list