title: SWIG C++ to Python: generate a subclass of Python list
body: <p>I am using SWIG (v3) to wrap C++ code to Python 3. In C++, I have a class <code>MyClass</code> which inherits from <code>std::vector</code>. I want to wrap this in Python, such that the resulting Python class <code>MyClass</code> is a subclass of the standard Python <code>list</code> class. Is this possible?</p>
  
  <p>Here is an example:</p>
  
  <p><strong>example.h</strong></p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  // List class
  class MyList : public vector&lt;int&gt; 
  {
      public:
          // Init
          MyList() : vector&lt;int&gt;() {
              this-&gt;insert(this-&gt;begin(),2);
              this-&gt;insert(this-&gt;begin(),1);
          }
  
          // Toy insert function
          void toy_insert() 
          {
              this-&gt;insert(this-&gt;begin(),0);
          }
  };
  </code></pre>
  
  <p><strong>example.cpp</strong></p>
  
  <pre><code>#include "example.h"
  </code></pre>
  
  <p><strong>example.i</strong></p>
  
  <pre><code>%module example
  
  %{
       #include "example.h"
  %}
  
  %include "std_vector.i"
  
  using namespace std;
  
  %template(MyVector) std::vector&lt;int&gt;; // Nothing known about base class 'vector&lt; int &gt;' if we remove std:: here
  
  %typemap(out) MyList* {
    int size = (*$1).size();
    $result = PyList_New(size);
  
    for (int i=0; i&lt;size; i++) {
      PyList_SetItem($result, i, PyInt_FromLong((*$1)[i]));
    }
  };
  
  // List class
  class MyList : public std::vector&lt;int&gt; // Nothing known about base class 'vector&lt; int &gt;' if we remove std:: here
  {
    public:
      MyList();
      void toy_insert();
  };
  </code></pre>
  
  <p><strong>run_example.py</strong></p>
  
  <pre><code>import example
  
  my_list = example.MyList()
  print(my_list)
  </code></pre>
  
  <p>The print command returns <code>&lt;example.MyList; proxy of [1, 2] &gt;</code>, but this is not what I want. Ideally this should just return <code>[1,2]</code>, but then I would still be able to call:</p>
  
  <pre><code>my_list.toy_insert()
  print(my_list)
  </code></pre>
  
  <p>which should return <code>[0,1,2]</code>.</p>
  
  <p>Thanks for your help </p>