When processing Python/c api numbers and strings, Python/c api provides the Py_BuildValue () function to convert numbers and strings, convert it to the corresponding data type in Python. The prototype of related functions is as follows.
PyObject * Py_BuildValue (const char * format ,...)
The parameter meanings are as follows.
Format: format the string,
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.
- Detailed introduction of scripts for reading and writing files in Python open
- Introduction to the actual content of the Python MD5 file generation code
- A detailed introduction to Python features
- Python history and positioning of Python in programming languages
- The Python design concept is also a new development of computer language applications
Python/c api List Operation
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: The 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: The index of the position of the item.
The Python/c api provides functions that correspond to list operations 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.