Discussion:
[Python] Custom __getattr__ with different CallPolicy for different return values
(too old to reply)
H Krishnan
2017-01-04 09:29:25 UTC
Permalink
Hi,

I would like to custom __getattr__ for my class such that for some
attributes, an internal reference is returned, and for others, a new object
is returned.

I cannot hardcode the attributes as they are dynamic.

I started writing something like:

boost::python::object myGetAttr(boost::python::object obj, std::string
const& attr)
{
MyObj* mobj = boost::python::extract<MyObj*>(obj)();
if (attr == "internal_ref")
{
MyRef* refObj = mobj->getInternalRef();
// how do I return this?
}
else
{
MyValueObj obj = mobj->getValueObj();
// how do I return this?
}
}

class_<MyObj, boost::noncopyable>
("MyObj", no_init)
.def ("__getattr__", &myGetAttr, ???what_should_I_put_here???)
;

However, I am not able to get this to work. Any suggestions?

Regards,
Krishnan

Loading...