How to modify object attribute by python C API
Huayang Xia
huayang.xia at gmail.com
Fri Jan 12 11:28:06 EST 2007
Thanks for the replies.
For the checking, I found we have to spend a lot of codes to check the
Python API's results. That is a pain. Is it reasonable to ignore the
result check of APIs like PyString_FromString and PyString_AsString
which will fail only when out of memory. Unfortunately there is no
exception thrown (in C++) so that we can integrate the error handling.
What is the best way(less code) to handle the error checking in normal
practice?
On Jan 11, 4:37 pm, "Huayang Xia" <huayang.... at gmail.com> wrote:
> I get a python object by running a class' constructor. Then I need to
> modify the instance's attribute just like obj.attr1.attr2 = 'a' if in
> python's term.
>
> PyObject* py_obj_attr1 = PyObject_GetAttrString(obj, "attr1");
> PyObject_SetAttrString(py_obj_attr1, "attr2",
> PyString_FromString("a"));
> Py_DECREF(py_obj_attr1);
>
> The object py_obj_attr1 is said to be a "New reference". It's
> confusing, does it refer to the same object as "obj.attr1" in python's
> term? So that the above code equals: obj.attr1.attr2 = 'a' in python's
> term.
>
> I
More information about the Python-list
mailing list