Boost logo

Boost Users :

Subject: [Boost-users] [Python] Custom __getattr__ with different CallPolicy for different return values
From: H Krishnan (hetchkay_at_[hidden])
Date: 2017-01-04 04:29:25


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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net