<div dir="ltr"><br>Thank you both for your help!!<br><br>@Matt: <br><br>WOW! I never new that you could create an iterable object from only the __len__ and operator[] functions!! I must have missed that when I was searching through the python documentation for iterator objects. It is not clear to me which is quicker, the only difference seems to be that when you use __iter__, you can return a copy of the object so that any modifications will not affect the original object. Thanks again!<br>
<br>@Giovanni: <br><br>That seems to be exactly what I was after. I have a few questions about the implementation though.<br><br>Since I do use the next() function as part of my C++ API i might just keep my iteration the way it is currently. I use it to iterate similar to the following code:<br>
<font face="courier new,monospace"><br>Item* Object::next()<br>{<br> if(this->currentIndex < this->length) {<br> return item[this->currentIndex];<br> } else {<br> return NULL;<br> }<br>}<br><br>// In Code<br>
Object* object = new Object;<br>Item* item;<br>while(item = object->next()) {<br> // do something with item<br>}<br><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">Oh!! So the %MethodCode doesn't require a partner C++ function? You can specify python specific functions inside it??<br>
<br>Would something like this work if I were to override the standard next() function and not implement a __iter__() function in the C++ codebase?<br><br><span style="font-family: courier new,monospace;">class Object</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{<br>.<br>.<br>.<br style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">%MethodCode</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Item* next()<br>{<br> </span></span></font><font face="courier new,monospace">if(sipCpp->currentIndex < sipCpp->length) {<br> Item *item = items<span style="font-family: courier new,monospace;">[sipCpp->currentIndex];<br>
sipCpp->currentIndex++;<br> return item;<br style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">
} else {</span></font><span style="font-family: courier new,monospace;"><br> PyErr_SetNone(PyExc_StopIteration);</span><font style="font-family: courier new,monospace;" face="courier new,monospace"><br> return NULL;<br>
}</font><br><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;">}<br><br>Object* __iter__()<br>{<br> return sipCpp;<br>}<br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">%End</span><br><br>I am sure I have some of the syntax screwed up in there (I wont be able to test the actual code till tomorrow). Or do I need to specify the functions above the %MethodCode like this:<br>
<br></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;">class Object</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{<br>
.<br>
.<br>
.</span><span style="font-family: courier new,monospace;"></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Item* next();</span></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">%MethodCode</span></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;"><br>
</span></span></font><font face="courier new,monospace">if(sipCpp->currentIndex < sipCpp->length) {<br><span style="font-family: courier new,monospace;">
return item[sipCpp->currentIndex];</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
} else {</span></font><span style="font-family: courier new,monospace;"><br>
PyErr_SetNone(PyExc_StopIteration);</span><font style="font-family: courier new,monospace;" face="courier new,monospace"><br>
return NULL;<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
}</span></font><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">%End</span><br style="font-family: courier new,monospace;"><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;">
<br>
Object* __iter__();</span></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;"></span></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">%MethodCode</span></span></font><font face="courier new,monospace"><span style="font-family: arial,helvetica,sans-serif;"><span style="font-family: courier new,monospace;"><br>
return sipCpp;<br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">%End<br><br><font face="arial,helvetica,sans-serif">I think that the only problem I have with the above solution is that the %MethodCode would be trying to access and set some private members... If there is no way around this I guess my only choice is to keep my next() implementation and use the __len__() and operator[] approach. <br>
<br>Thanks again!!<br><br>- Jonny <br></font></span></span></font></div>