title: Boost.Python return python object which references to existing c++ objects
body: <p>Suppose I want to get the reference to some global / internal c++ object, one method is to declare function with <code>boost::python::return_value_policy&lt;reference_existing_object&gt;()</code>.</p>
  
  <p>Both <code>GetGlobalObjectA</code> and <code>GetGlobalObjectB</code> return the reference to the original c++ object without create a new copy;</p>
  
  <p>But how to make <code>GetGlobalObjectByID</code> return a ref to the existing c++ object?</p>
  
  <pre>
  struct A { uint32_t value; };
  struct B { uint64_t value; };
  
  A globalA;
  B globalB;
  
  boost::python::object GetGlobalObjectByID(int id)
  {
      // boost::python::object will return a new copy of C++ object, not the global one.
  
      if (id == 1)
          return boost::python::object(&globalA);
      else if (id == 2)
          return boost::python::object(&globalB);
      else
          return boost::python::object(nullptr);
  }
  
  A& GetGlobalObjectA() { return globalA; }
  B& GetGlobalObjectB() { return globalB; }
  
  BOOST_PYTHON_MODULE(myModule)
  {
      using namespace boost::python;
  
      class_&ltA&gt("A");
      class_&ltB&gt("B");
  
      def("GetGlobalObjectByID", GetGlobalObjectByID);
  
      def("GetGlobalObjectA", GetGlobalObjectA, return_value_policy&ltreference_existing_object&gt());
      def("GetGlobalObjectB", GetGlobalObjectB, return_value_policy&ltreference_existing_object&gt());
  
  }
  
  </pre>