# How to make a C++ object iterable in Python

**URL:** <https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265>\
**Category:** Support\
**Created:** [September 28, 2015, 11:59pm UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265 "2015-09-28T23:59:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![boutigny](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/boutigny/32/26_2.png) [@boutigny](https://www.rubin.community/u/boutigny)\
**Post date:** [September 28, 2015, 11:59pm UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/1 "2015-09-28T23:59:42Z")

</div>

I have the following C++ class :  
`class CcdImageList : public std::list<CountedRef<CcdImage> >`  
The Swig object is correctly transmitted to the Python, but I cannot iterate on the list.  
What should I do (in Swig ? ) to make it iterable ?

---

<div class="post-metadata">

**Author:** ![ktl](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/ktl/32/1373_2.png) [@ktl](https://www.rubin.community/u/ktl)\
**Post date:** [September 29, 2015, 1:07pm UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/2 "2015-09-29T13:07:55Z")

</div>

Did you `%include "std_list.i"` (or `%include "p_lsstSwig.i"`, which does that for you)?

Did you `%template(SomeName) std::list<CountedRef<CcdImage>>` before `%include`ing your .h file?

`CcdImageList` should inherit its ` __iter__ ` method from `SomeName`.

---

<div class="post-metadata">

**Author:** ![boutigny](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/boutigny/32/26_2.png) [@boutigny](https://www.rubin.community/u/boutigny)\
**Post date:** [September 29, 2015, 6:20pm UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/3 "2015-09-29T18:20:36Z")

</div>

I confirm that I have %include “p\_lsstSwig.i” and the %template is located before the %include

I don’t understand “CcdImageList should inherit its \__iter_\_ method from SomeName” ? Is “SomeName” the same as the one in “%template(SomeName)” ?

The problem is that I don’t understand what I am doing. Can you recommend some introductory Swig documentation to at least understand the basics ?

---

<div class="post-metadata">

**Author:** ![ktl](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/ktl/32/1373_2.png) [@ktl](https://www.rubin.community/u/ktl)\
**Post date:** [September 30, 2015, 5:09am UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/4 "2015-09-30T05:09:35Z")

</div>

> [@boutigny](#):
>
> Can you recommend some introductory Swig documentation to at least understand the basics ?

You might try [SWIG and C++](http://swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_nn30)

But it might take looking at the generated code to figure out what the problem is.

---

<div class="post-metadata">

**Author:** ![rowen](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/rowen/32/52_2.png) [@rowen](https://www.rubin.community/u/rowen)\
**Post date:** [September 30, 2015, 5:51pm UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/5 "2015-09-30T17:51:06Z")

</div>

As far as "CcdImageList should inherit its ` __iter__ ` method from `SomeName`" I think K-T just means that when you use this template then SWIG will stop treating such objects as opaque and will allow you to iterate over them. if `std_list.i` works like `std_vector.i` then the result is what you want: you can examine C++ lists of this type and for calling functions that take such a list as an argument you may even be able to provide an ordinary python lists whose elements are `CountedRef<CcdImage>>` instead of providing a genuine `std::list<CountedRef<CcdImage>>` (the vector wrapper certainly allows that). If not, you can construct a “`SomeName`” (please pick a better name, such as `CcdImageRefList`) and append elements to it one at a time, then pass that.

---

<div class="post-metadata">

**Author:** ![boutigny](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/boutigny/32/26_2.png) [@boutigny](https://www.rubin.community/u/boutigny)\
**Post date:** [October 1, 2015, 12:14am UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/6 "2015-10-01T00:14:09Z")

</div>

After several iteration with @ktl and @rowen we finally solved this. The Swig interface should contain:

```auto
%include "lsst/meas/simastrom/CountedRef.h"
%template(CcdImageCountedRef) lsst::meas::simastrom::CountedRef<lsst::meas::simastrom::CcdImage>;
%template(CcdImageCountedRefList) std::list<lsst::meas::simastrom::CountedRef<lsst::meas::simastrom::CcdImage> >;
%include "lsst/meas/simastrom/CcdImage.h"

```

In this order. The bottom line is that Swig is processing the .i file in one single pass, so every “object” should be known before being used.

---

<div class="post-metadata">

**Author:** ![rowen](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/rowen/32/52_2.png) [@rowen](https://www.rubin.community/u/rowen)\
**Post date:** [October 1, 2015, 12:32am UTC](https://www.rubin.community/t/how-to-make-a-c-object-iterable-in-python/265/7 "2015-10-01T00:32:35Z")

</div>

I will also add that there was unhappiness with using CountedRef instead of a shared\_ptr, so @jbosch has kindly agreed to help decide if shared\_ptr can do all that is required (and in a way that doesn’t confuse SWIG too badly).
