[Python-iterators] While we're at it...
Thomas Wouters
thomas at xs4all.net
Mon Jul 2 03:22:39 EDT 2001
On Mon, Jul 02, 2001 at 11:03:18AM +1000, Delaney, Timothy wrote:
> > >Of course, an iterator map would be useful:
> > >
> > >def iter_map(f, seq):
> > > for o in seq:
> > > yield f(o)
> >
> > This is a generator, not just an iterator.
> def iter_map (f, seq):
>
> def iter_map_generator (f=f, seq=seq):
> for o in seq:
> yield f(o)
> return iter_map_generator()
While this makes a technical difference, the result is exactly the same.
It's just slower :-) The first example (iter_map, the generator) shows
exactly why 'yield' isn't magic and a generator should be defined using
'def': all you need is 'yield' instead of 'result.append()' and you
magically do the right thing :-)
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list