title: How to use a Python list to assign a std::vector in C++ using SWIG?
body: <p>I have a simple C++ class that contains a std::vector member and a member function that takes a std::vector as an argument that I am wrapping with SWIG and calling from Python.  The example code is below.</p>
  
  <p>After compiling it, I go into Python and do:</p>
  
  <pre><code>import test
  t = test.Test()
  a = [1, 2, 3]
  b = t.times2(a) # works fine
  t.data = a # fails!
  </code></pre>
  
  <p>The error message I get is:</p>
  
  <pre><code>TypeError: in method 'Test_data_set', argument 2 of type 'std::vector&lt; double,std::allocator&lt; double &gt; &gt; *'
  </code></pre>
  
  <p>I know that I can just do:</p>
  
  <pre><code>t.data = test.VectorDouble([1,2,3])
  </code></pre>
  
  <p>But I would like to know how to just use a Python list directly in the assignment, or at least understand why it doesn't work.</p>
  
  <hr>
  
  <p>Here's the example code.</p>
  
  <p>test.i:</p>
  
  <pre><code>%module test
  
  %include "std_vector.i"
  
  namespace std {
      %template(VectorDouble) vector&lt;double&gt;;
  };
  
  %{
  #include "test.hh"
  %}
  
  %include "test.hh"
  </code></pre>
  
  <p>test.hh:</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class Test {
      public:
          std::vector&lt;double&gt; data;
          std::vector&lt;double&gt; times2(std::vector&lt;double&gt;);
  };
  </code></pre>
  
  <p>test.cc:</p>
  
  <pre><code>#include "test.hh"
  
  std::vector&lt;double&gt;
  Test::times2(
      std::vector&lt;double&gt; a)
  {
      for(int i = 0; i &lt; a.size(); ++i) {
          a[i] *= 2.0;
      }
      return a;
  }
  </code></pre>
  
  <p>makefile:</p>
  
  <pre><code>_test.so: test.cc test.hh test.i
      swig -python -c++ test.i
      g++ -fpic -shared -o _test.so test.cc test_wrap.cxx -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -L/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/ -lpython2.7
  </code></pre>