If you want to use Python to embed C/C ++ into a dictionary, you must understand the functions you need when creating a dictionary, such as PyDict_New (), the following article describes how to use functions to embed C/C ++ dictionaries in Python.
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.
- Python Thread Programming solves the problem of different speeds in asynchronous threads
- Python embedded in C/C + has very powerful application functions
- Python unicode ascii code on windows
- Introduction to two common functions in lower-level Python embedding
- A detailed introduction to the operations on tuples embedded in C/C ++ using Python
· P: the dictionary for operations.
· Key: the keyword for adding an item. For the PyDict_SetItem () function, it is of the PyObject type, and for the PyDict_SetItemString () function, it is of the char type.
· Val: 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. When Python is embedded into a dictionary operation in C/C ++, 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 the PyDict_DelItem () function, it is of the PyObject type, and for the PyDict_DelItemString () function, it is of the 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: return 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 content is a detailed introduction to the dictionary operations embedded in Python in C/C ++.