[C++-sig] return reference to same python object

Jonge, W. de (Cebra) W.d.Jonge at cebra.tue.nl
Wed Jan 4 12:34:11 CET 2006


Hi,

I have interfaced a C++ class to Python, and have a function to get to
existing C++ objects. However every time a retrieval for a C++ object is
done a new Python wrapper object is constructed and I need this to be a
reference to the same python object so I can add attributes in Python
that will still exist when a new retrieval for the object is done.

I have something like this in C++:

typedef boost::shared_ptr<VT_Client> ClientPtr;

class VT_Client : public boost::enable_shared_from_this<VT_Client>
{
...
}

ClientPtr getClient(const std::string& username);

def("getClient", getClient);


When I now retrieve an object in Python, and assign an extra attribute:

a = getClient("joe");
a.some_attribute = 1

I will lose the python object and attribute when a goes out of scope.
What I need is that the underlying python object stays alive and that
that is returned next time instead of a new one. So later on in my
script I want to be able to do:

b = getClient("joe")
print b.some_attribute # should work here, but doesn't as explained
above

How can I make this work?

Any help is greatly appreciated.

Thanks,

Willem





More information about the Cplusplus-sig mailing list