'파이썬 C/api'에 해당되는 글 1건

  1. 2010.04.06 python C/api - PyObject_GetAttrString()
PyObject_GetAttrString() 함수는 파이썬 내의 변수 객체를 받아오는 녀석이다.

PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)
Return value: New reference.

Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name.


[링크 : http://docs.python.org/c-api/object.html#PyObject_GetAttrString]

문제는 PyObject *o 인데, o는 전체를 의미하는 __main__을 사용하면 될듯하다.
아무튼 간략하게 사용하자면, 이런식으로 문자열을 출력 가능하다.

    Py_Initialize();
        PyRun_SimpleString("teststr=\"test test\"");

        PyObject* po_main = PyImport_AddModule("__main__");
        PyObject* po_dict = PyObject_GetAttrString(po_main, "teststr");

        printf("teststr [%s]\n",PyString_AsString(po_dict));

        Py_DECREF(po_main);
        Py_DECREF(po_dict);

    Py_Finalize();


[링크 : http://koichitamura.blogspot.com/2008/06/this-is-small-python-capi-tutorial.html]
Posted by 구차니