Using iterators to write in the structure being iterated through?

Chris Lambacher chris at kateandchris.net
Wed Jul 26 12:45:21 EDT 2006


I think you are going to need to provide some python minimal code as an
example of what is not working for you.

-Chris
On Wed, Jul 26, 2006 at 11:39:36AM -0400, Pierre Thibault wrote:
> Hello!
> 
> I am currently trying to port a C++ code to python, and I think I am stuck
> because of the very different behavior of STL iterators vs python
> iterators. What I need to do is a simple arithmetic operations on objects
> I don't know. In C++, the method doing that was a template, and all that
> was required is that the template class has an iterator conforming to the
> STL forward iterator definition. Then, the class would look like:
> 
> template <class H>
> class MyClass
> {
> public:
> 
> 	MyClass(H& o1, H& o2) : object1(o1), object2(o2) {}
> 
> 	void compute();
> 
> private:
> 	H& object1;
> 	H& object2;
> 
> };
> 
> template <class H>
> void MyClass::compute()
> {
> 	typedef typename H::iterator I;
> 
> 	I o1_begin = object1.begin();
> 	I o2_begin = object2.begin();
>         I o1_end = object1.end();
> 
> 	for(I io1 = o1_begin, io2 = o2_begin; io1 != o1_end; ++io1, ++io2)
>  	{
> 		// Do something with *io1 and *io2, for instance:
> 		// *io1 += *io2;
> 	}
> }
> 
> This is all nice: any object having a forward iterator works in there.
> 
> Then I discovered python and wanted to use all its goodies. I thought it
> would be easy to do the same thing but I can't: the iterator mechanism is
> read-only, right? So it does no make sense to write:
> 
> io1 = iter(object1)
> io2 = iter(object2)
> 
> try:
>   while 1:
>     io1.next() += io2.next()
> except StopIteration:
>   pass
> 
> That won't work:
> SyntaxError: can't assign to function call
> 
> Here is my question: how could I do that and retain enough generallity?
> 
> Thanks!
> 
> Pierre
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list