Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPython - Read Python Dictionary (keys/values) inside a C Function Passed as argument

Tags:

python

cpython

I am writing a Python C extension. I am passing a Python dict to a C function. I am able to parse it, using the following code:

PyObject *large_dict = NULL;
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &large_dict)) return NULL;
if (large_dict != NULL)
{
   printf("Large Dictionary Not Null\n");
}

Here the statement "Large Dictionary Not Null" is printed, which means that the dictionary is parsed successfully. Now I want to access the dictionary values by specifying keys, like we do in Python. For example, large_dict['k1'] would give value v1.

How can I access dictionary keys/values inside this C function?

like image 686
Muhammad Sadiq Alvi Avatar asked Dec 24 '15 09:12

Muhammad Sadiq Alvi


People also ask

How do you pass a dictionary item as a function argument in Python?

“ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.

How do you get a value from a nested dictionary Python?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

What does dict keys () do in Python?

The methods dict. keys() and dict. values() return lists of the keys or values explicitly. There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary.

Are Python Dicts passed by value?

By definition python passes its immutable objects by value, and its mutable objects by reference. Answer could be improved by pointing out that strings are immutable and dictionaries are mutable.


1 Answers

You should go through the link, https://docs.python.org/2/c-api/dict.html Excerpt given below,

PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
Return value: Borrowed reference.
Return the object from dictionary p which has a key key. Return NULL if the key key is not present, but without setting an exception.

PyObject* PyDict_GetItemString(PyObject *p, const char *key)
Return value: Borrowed reference.
This is the same as PyDict_GetItem(), but key is specified as a char*, rather than a PyObject*.

PyObject* PyDict_Items(PyObject *p)
Return value: New reference.
Return a PyListObject containing all the items from the dictionary, as in the dictionary method dict.items().

PyObject* PyDict_Keys(PyObject *p)
Return value: New reference.
Return a PyListObject containing all the keys from the dictionary, as in the dictionary method dict.keys().

PyObject* PyDict_Values(PyObject *p)
Return value: New reference.
Return a PyListObject containing all the values from the dictionary p, as in the dictionary method dict.values().

Keep an eye on borrowed reference / new reference. It is little tricky while coding for Python extensions.

like image 155
Aashish P Avatar answered Sep 16 '22 23:09

Aashish P