At Thursday 11/1/2007 18:37, Huayang Xia 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.


Read the Introduction in the "Python/C API Reference Manual". If you plan to use Python from C code, better learn carefully how reference counting works, or your progam won't work at all (or crash, in unrelated places, at a later time...) Going back to the example, yes, it's like obj.attr1.attr2 = 'a'. But you should check that py_obj_attr1 is not NULL, and the same for PyString_FromString. There are examples in the doc cited and in "Extending and Embedding".


--
Gabriel Genellina
Softlab SRL

        

        
                
__________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to