Embed Python into C/C ++ at a lower level, or if you want to get the values returned by a Python script, if you want to write a C program using related functions, you want to have a better understanding of how to embed Python into C/C ++ at a lower level, you can browse the following articles. Because Python has its own data type, you must use a special API to operate the corresponding data type in the C program. Common functions are as follows.
Number and string processing
The Python/c api provides the Py_BuildValue () function to convert numbers and strings to the corresponding data type in Python. The function prototype is as follows.
PyObject * Py_BuildValue (const char * format ,...)
The parameter meanings are as follows.
· Format: format the string, as shown in Table 8-1.
- "IBM Forum 2010" held a grand smart theme
- Win the data war with IBM's wisdom
- Start with IBM's "Smart City"
- Interview with CTO of boutong enterprise network division: upgrade to cloud computing
- Introduction to the powerful functions generated when Python is embedded into C/C ++
The remaining parameters in the Py_BuildValue () function are integer, floating point, or string values in the C language to be converted. The returned value is a pointer of the PyObject type. In C, all Python types are declared as PyObject.
List operations
Python/c api provides the PyList_New () function to create a new Python list. The Return Value of the PyList_New () function is the list created. The function prototype is as follows.
PyObject * PyList_New (Py_ssize_t len)
The parameter meanings are as follows.
· Len: the length of the created list. After the list is created, you can use the PyList_SetItem () function to add items to the list. The function prototype is as follows.
- int PyList_SetItem( PyObject *list, Py_ssize_t index,
PyObject *item)
The parameter meanings are as follows.
· List: list of items to be added.
· Index: Location index of the added item.
· Item: the value of the added item.
You can also use the PyList_GetItem () function in Python/c api to obtain the value of an item in the list. The value returned by the PyList_GetItem () function. The function prototype is as follows.
PyObject * PyList_GetItem (PyObject * list, Py_ssize_t index)
The parameter meanings are as follows.
· List: list of operations to be performed.
· Index: index of the position of an item.
Python embedded in C/C ++) in the actual operation process, the Python/c api provides the function corresponding to the List Operation in Python. For example, the List append method corresponds to the PyList_Append () function. The sort method of the list corresponds to the PyList_Sort () function. The reverse Method of the list corresponds to the PyList_Reverse () function. The function prototype is as follows.
Int PyList_Append (PyObject * list, PyObject * item)
Int PyList_Sort (PyObject * list)
Int PyList_Reverse (PyObject * list)
For the PyList_Append () function, the parameter meanings are as follows.
· List: list of operations to be performed.
· Item: the item to participate in.
For PyList_Sort () and PyList_Reverse () functions, the parameter meanings are the same.
· List: list of operations to be performed.
The above is an introduction to Python embedded in C/C ++ at a lower level.