Iterator question

Raymond Hettinger vze4rx4y at verizon.net
Sat May 17 10:51:46 EDT 2003


> Neal> I was wondering if python could imitiate the c++ stl copy function,
using
> Neal> python iterators.  It looks like the answer is no, because iterators
only
> Neal> support next(), and next returns an "rvalue", not an "lvalue", so there
is
> Neal> no way to assign through an iterator.  Is this correct?

> "Andrew Koenig" <ark at research.att.com> wrote in message
> Somehow you would need to externd the Python iterators to cover output.
> One way of doing so, which would be very different from how C++ does it,
> is to define algorithms that produce output by having them *return* an
> iterator that the caller can then use.
>
> This idea is only partly baked, and needs additional cooking in order
> to be edible...

To use iterators as producers instead of consumers, you need a way
to send data into the iterator.  PEP 288 proposes attributes as the
simplest mechanism for passing in data:
   http://www.python.org/peps/pep-0288.html

Though generators do not currently allow attributes, the approach
can be efficiently and concisely simulated with a class wrapper.
The whole process can be reduced to a single conveinient function
call.  See the cookbook recipe at:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/164044


Raymond Hettinger






More information about the Python-list mailing list