title: How to create a Python Object from a C++ Object using Boost.Python
body: <p>I have a C++ project that use a lot of classes. The main one is '<code>sso::Object</code>' (every classes are in the 'sso' namespace) and this class is derived into some other classes but one which is abstract: '<code>sso::Drawable</code>'.</p>
  
  <p>This class has two pure virtual methods '<code>sso::Drawable::set_opacity</code>' and '<code>sso::Drawable::raw_draw</code>' and it is derived into other classes like '<code>sso::View</code>' which implement these two methods.</p>
  
  <p>The whole project works fine when it is used in C++, but I would like to use it in Python too, so I created a Boost.Python module like that:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class DrawableWrapper : public sso::Drawable , public wrapper&lt;sso::Drawable&gt; {
  public:
      void set_opacity(byte opacity) { this-&gt;get_override("set_opacity")(opacity); }
  
      void raw_draw(const sso::Rect &amp;rect,sso::Target &amp;target,const sso::Position &amp;position) const {
          this-&gt;get_override("raw_draw")(rect,target,position);
      }
  };
  
  BOOST_PYTHON_MODULE(sso) {
  
      class_&lt;DrawableWrapper,boost::noncopyable&gt; ("Drawable",init&lt;&gt;())
          .add_property ("opacity",&amp;sso::Drawable::get_opacity)
          // python_sso_getter &amp; python_sso_setter_* are only used for an easier access to accessors
          .add_property ("position",python_sso_getter&lt;sso::Drawable,sso::Position,&amp;sso::Drawable::get_position&gt;,python_sso_setter_1_const&lt;sso::Drawable,sso::Position,&amp;sso::Drawable::set_position&gt;)
          .def("raw_draw",pure_virtual(&amp;sso::Drawable::raw_draw))
     ;
  
      class_&lt;sso::View,bases&lt;sso::Drawable&gt; &gt;                 ("View",init&lt;&gt;())
          .def("insert",python_sso_setter_1&lt;sso::View,sso::Drawable*,&amp;sso::View::insert&gt;)
          .def("remove",&amp;sso::View::erase)
      ;
  
  }
  </code></pre>
  
  <p>This code compile without errors but when I execute these lines in Python:
  </p>
  
  <pre><code>myview = sso.View()
  print myview
  </code></pre>
  
  <p>I get this output:</p>
  
  <blockquote>
    <p>&lt;sso.View object at 0x7f9d2681a4b0&gt;</p>
  </blockquote>
  
  <p>But my C++ debugger tell me that the variable 'v' (the python 'myview') is a '<code>sso::Object</code>' instance, not a '<code>sso::View</code>' one. <code>sso::View::View()</code> is called but the variable type is not a view and I don't know why. Do you have any idea about that ? Did you do something like that and found a way to make it work ?</p>
  
  <p>I use Python2.7 and Boost.Python1.49 and gcc version 4.6.1</p>
  
  <p>EDIT: I've made a mistake: <code>sso::Drawable</code> does not inherit from <code>sso::Object</code>, but <code>sso::View</code> does (=multiple inheritance).</p>