title: How to make a C++ class iterable from Python using SWIG?
body: <p>I have a C++ class <code>Collection</code> that manages a <code>std::vector&lt;Element&gt;</code> (a private member of the  class).</p>
  
  <p>From C++ I can iterate through the vector using the <code>begin()</code> and <code>end()</code> iterators (which are just typedefs for the <code>vector</code>'s iterators) like:</p>
  
  <pre><code>Collection col;
  for (Collection::const_iterator itr = col.begin(); itr != col.end(); itr++)
  {
    std::cout &lt;&lt; itr-&gt;get() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Now I wish to do a similar thing from Python like:</p>
  
  <pre><code>import example
  el = example.Element()
  el.set(5)
  col = example.Collection()
  col.add(el)
  for e in col:
      print e.get()
  </code></pre>
  
  <p>But this results in:</p>
  
  <blockquote>
    <p>TypeError: 'Collection' object is not iterable</p>
  </blockquote>
  
  <p>I am not able to configure SWIG in a way that it generates the <code>__iter__</code> (I think it's the only thing it needs) for the Python <code>Collection</code> class. How am I supposed to do this?</p>
  
  <p>This is my code:</p>
  
  <p><em>example.h:</em></p>
  
  <pre><code>#include &lt;vector&gt;
  
  class Element
  {
  public:
    Element();
    ~Element();
  
    int get() const;
    void set(const int var);
  
  private:
    int variable_;
  };
  
  class Collection
  {
  public:
    Collection();
    ~Collection();
  
    void add(const Element&amp; element);
  
    typedef std::vector&lt;Element&gt; tElements;
  
    // iterators
    typedef tElements::iterator iterator;
    typedef tElements::const_iterator const_iterator;
    iterator begin();
    const_iterator begin() const;
    iterator end();
    const_iterator end() const;
  
  private:
    tElements          elements_;
  };
  </code></pre>
  
  <p><em>example.cpp:</em></p>
  
  <pre><code>#include "example.h"
  
  Element::Element() {}
  
  Element::~Element() {}
  
  int Element::get() const
  {
    return variable_;
  }
  
  void Element::set(const int var)
  {
    variable_ = var;
  }
  
  Collection::Collection() : elements_() {}
  
  Collection::~Collection() {}
  
  void Collection::add(const Element&amp; element)
  {
    elements_.push_back(element);
  }
  
  Collection::iterator Collection::begin()
  {
    return elements_.begin();
  }
  
  Collection::const_iterator Collection::begin() const
  {
    return elements_.begin();
  }
  
  Collection::iterator Collection::end()
  {
    return elements_.end();
  }
  
  Collection::const_iterator Collection::end() const
  {
    return elements_.end();
  }
  </code></pre>
  
  <p><em>example.i:</em></p>
  
  <pre><code>%module example
  %{
  #include "example.h"
  %}
  
  // I've tried to add this, but that generates a whole
  // other class, that is not what I want.
  // %include "std_vector.i"
  // %template(ElementVector) std::vector&lt;Element&gt;;
  
  // I've also tried to %extend the class (which I think is what I want,
  // but I cannot figure out with what to extend it with)
  
  // Include the header file with above prototypes
  %include "example.h"
  </code></pre>
  
  <p><em>Compile with:</em></p>
  
  <pre><code>swig -python -c++ -o example_wrap.cpp example.i
  g++ -fPIC -c example.cpp example_wrap.cpp -I/usr/include/python2.6
  g++ -shared example.o example_wrap.o -o _example.so
  </code></pre>