[C++-sig] Embedding Python - creating Python objects from C++

Sohail Somani sohail at taggedtype.net
Mon Nov 30 01:50:13 CET 2009


I have a class in C++ as follows:

class Base
{
   virtual ~Base() /* ideally like = 0 but doesn't seem to work */;
};

I export this class to Python:

class_<Base>("Base",no_init);

The user is expected to create a derived class:

class UserClass(Base):
   ... stuff here ...


What I want to do then (in C++) is create instances of this Python class 
that are stored using a shared pointer:

  // constructs a new instance of UserClass in Python using magic
  // but uses shared_ptr to keep alive
shared_ptr<Base> base(/* magic here */);

I understand that I can do something like module.attr("UserClass")() to 
create an instance but that will return an object instance which doesn't 
seem like it would play nice if I extracted a Base pointer from it.

So does anyone know how to implement the above referenced magic? It 
sounds like it should be obvious, but I'm not sure.

Thanks, and sorry if this is a newb question!

Sohail



More information about the Cplusplus-sig mailing list