title: How to extend c++ class in python with operator [], using swig
body: <p>I have a simple test.h file with my own array class (which uses the standard vector class):</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Array1D{
  private:
     vector&lt;double&gt; data_; 
     int xsize_; 
  public:
      Array1D(): xsize_(0) {};
  
      // creates vector of size nx and sets each element to t
      Array1D(const int&amp; nx, const double&amp; t): xsize_(nx) {
         data_.resize(xsize_, t);
      }
  
      double&amp; operator()(int i) {return data_[i];}
      const double&amp; operator[](int i) const {return data_[i];}
  
  };
  </code></pre>
  
  <p>I want to be able to use the [] operator in python using swig. My current SWIG interface file looks like </p>
  
  <pre><code> %module test
  
   %{ 
   #define SWIG_FILE_WITH_INIT 
   #include "test.h"
   %}
  
   %include "std_vector.i"
  
   namespace std{
  %template(DoubleVector) vector&lt;double&gt;;
   }
  
   %include "test.h"
  </code></pre>
  
  <p>When I make the module, everything runs fine, but when I instantiate an object of Array1D, a = test.Array1D(10,2), which creates a length 10 vector with 2 in each element, and type a[1] I get 
  <code>TypeError: 'Array1D' object does not support indexing</code>. </p>
  
  <p>How should my SWIG interface file look in order to extend the operator method so I can output a[1] properly within python? I would also like to be able to do something like a[1] = 3.0;  </p>