Hi,
I’m new to writing extensions and things were going well until I needed a custom getattr on a class. Each Python object wraps a large chunk of C++ code that does a Monte-Carlo simulation, so I will need a getattr that can “reach through” into the underlying objects for the output values. This has been going well with simple types, but anything that I’ve simply stored in the class as a Python object (typically strings) keeps getting corrupted. I’ve looked over the documentation on extension modules and I can’t find a simple example of custom getattr function.
Here’s a snippet of the code. I’m wondering if I need to do something around reference counting?
static PyObject *
MarketSegmentObject_getattr(MarketSegmentObject *obj, char *attr_name)
{
if (strcmp(attr_name, "segment") == 0) {
// This is a PyObject* with a string in it that's setup in the init
// This seems to be causing me trouble
return obj->segment;
}
else if (strcmp(attr_name, "base_demand") == 0) {
// This seems to work fine
return PyFloat_FromDouble(obj->baseDemand);
}
}
Regards,
Alan
