How to modify object attribute by python C API

Huayang Xia huayang.xia at gmail.com
Thu Jan 11 22:37:08 CET 2007


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