If you are interested in embedding Python in the tuples of C/C ++, you can click the following articles to learn about it, the following articles will introduce the specific application of the tuples embedded in C/C ++ in Python and the related functions used in the operations.
Embedding Python into the tuples of C/C ++
The Python/c api provides the PyTuple_New () function to create a new Python tuples. The PyTuple_New () function returns the created tuples. The function prototype is as follows.
PyObject * PyTuple_New (Py_ssize_t len)
The parameter meanings are as follows.
Len: the length of the created tuples.
After the tuples are created, you can use the PyTuple_SetItem () function to add items to the tuples. The function prototype is as follows.
- Analysis of Tailspin Travel U in ASP. net mvc 2
- Define future Web style CSS 3 latest features
- Python embeds C ++ to make up for the shortcomings of C ++.
- Introduction to the powerful functions generated when Python is embedded into C/C ++
- Python embedded in C/C ++ at a lower level)
Int PyTuple_SetItem (PyObject * p, Py_ssize_t pos, PyObject * o)
The parameter meanings are as follows.
P: The operation tuples.
Pos: The Position Index of the added item.
O: the added item value.
You can use the PyTuple_GetItem () function in Python/c api to obtain the value of an item in the tuples. The value of the return item of the PyTuple_GetItem () function. The function prototype is as follows.
PyObject * PyTuple_GetItem (PyObject * p, Py_ssize_t pos)
The parameter meanings are as follows.
P: The tuples to be operated.
Pos: The Position Index of the item.
After the tuples are created, you can use the _ PyTuple_Resize () function to re-adjust the size of the tuples. The function prototype is as follows.
Int _ PyTuple_Resize (PyObject ** p, Py_ssize_t newsize)
The parameter meanings are as follows.
P: pointer to the tuples to be operated.
Newsize: the size of the new element group.
Embedding Python into a dictionary
Python/c api provides the PyDict_New () function to create a new dictionary. The PyDict_New () function returns the dictionary created. The function prototype is as follows.
PyObject * PyDict_New ()
After creating a dictionary, you can use the PyDict_SetItem () function and the PyDict_SetItemString () function to add an item to the dictionary. The function prototype is as follows.
Int PyDict_SetItem (PyObject * p, PyObject * key, PyObject * val)
Int PyDict_SetItemString (PyObject * p, const char * key, PyObject * val)
The parameter meanings are as follows.
P: the dictionary for operations.
Key: the keyword for adding an item. For PyDict_SetItem (), the function is in PyObject type, and for PyDict_SetItemString (), the function is in char type.
Val: the value of the added item.
Use the PyDict_GetItem () and PyDict_GetItemString () functions in the Python/c api to obtain the value of an item in the dictionary. They all return the value of the item. The function prototype is as follows.
PyObject * PyDict_GetItem (PyObject * p, PyObject * key)
PyObject * PyDict_GetItemString (PyObject * p, const char * key)
The parameter meanings are as follows.
P: the dictionary for operations.
Key: the keyword for adding an item. For the PyDict_GetItem () function, it is of the PyObject type, and for the PyDict_GetItemString () function, it is of the char type.
You can use the PyDict_DelItem () and PyDict_DelItemString () functions in the Python/c api to delete an item in the dictionary. The function prototype is as follows.
Int PyDict_DelItem (PyObject * p, PyObject * key)
Int PyDict_DelItemString (PyObject * p, char * key)
The parameter meanings are as follows.
P: the dictionary for operations.
Key: the keyword for adding an item. For PyDict_DelItem (), the function is in PyObject type, and for PyDict_DelItemString (), the function is in char type.
Use the PyDict_Next () function in Python/c api to traverse the dictionary. The function prototype is as follows.
Int PyDict_Next (PyObject * p, Py_ssize_t * ppos, PyObject ** pkey, PyObject ** pvalue)
The parameter meanings are as follows.
P: the dictionary to be traversed.
Ppos: the position of the item in the dictionary. It should be initialized to 0.
Pkey: returns the dictionary keyword.
Pvalue: returns the dictionary value.
The Python/c api provides functions that correspond to dictionary operations in Python. For example, the item method of the dictionary corresponds to the PyDict_Items () function. The dictionary keys method corresponds to the PyDict_Keys () function. The values method of the dictionary corresponds to the PyDict_Values () function. The function prototype is as follows.
PyObject * PyDict_Items (PyObject * p)
PyObject * PyDict_Keys (PyObject * p)
PyObject * PyDict_Values (PyObject * p)
The parameter meanings are as follows.
P: the dictionary for operations.
The above is an introduction to how to embed Python into the tuples of C/C ++ and the content related to the Python embedded dictionary operation. I hope you will gain some benefits.