mapping.rst 2.61 KB
Newer Older
1 2 3 4 5 6 7 8
.. highlightlang:: c

.. _mapping:

Mapping Protocol
================


9
.. c:function:: int PyMapping_Check(PyObject *o)
10 11 12 13 14

   Return ``1`` if the object provides mapping protocol, and ``0`` otherwise.  This
   function always succeeds.


15
.. c:function:: Py_ssize_t PyMapping_Size(PyObject *o)
16
               Py_ssize_t PyMapping_Length(PyObject *o)
17 18 19 20 21 22 23 24

   .. index:: builtin: len

   Returns the number of keys in object *o* on success, and ``-1`` on failure.  For
   objects that do not provide mapping protocol, this is equivalent to the Python
   expression ``len(o)``.


25
.. c:function:: int PyMapping_DelItemString(PyObject *o, const char *key)
26 27 28 29 30

   Remove the mapping for object *key* from the object *o*. Return ``-1`` on
   failure.  This is equivalent to the Python statement ``del o[key]``.


31
.. c:function:: int PyMapping_DelItem(PyObject *o, PyObject *key)
32 33 34 35 36

   Remove the mapping for object *key* from the object *o*. Return ``-1`` on
   failure.  This is equivalent to the Python statement ``del o[key]``.


37
.. c:function:: int PyMapping_HasKeyString(PyObject *o, const char *key)
38 39 40 41 42 43

   On success, return ``1`` if the mapping object has the key *key* and ``0``
   otherwise.  This is equivalent to the Python expression ``key in o``.
   This function always succeeds.


44
.. c:function:: int PyMapping_HasKey(PyObject *o, PyObject *key)
45 46 47 48 49 50

   Return ``1`` if the mapping object has the key *key* and ``0`` otherwise.  This
   is equivalent to the Python expression ``key in o``.  This function always
   succeeds.


51
.. c:function:: PyObject* PyMapping_Keys(PyObject *o)
52 53

   On success, return a list of the keys in object *o*.  On failure, return *NULL*.
Ezio Melotti's avatar
Ezio Melotti committed
54
   This is equivalent to the Python expression ``list(o.keys())``.
55 56


57
.. c:function:: PyObject* PyMapping_Values(PyObject *o)
58 59

   On success, return a list of the values in object *o*.  On failure, return
Ezio Melotti's avatar
Ezio Melotti committed
60
   *NULL*. This is equivalent to the Python expression ``list(o.values())``.
61 62


63
.. c:function:: PyObject* PyMapping_Items(PyObject *o)
64 65 66

   On success, return a list of the items in object *o*, where each item is a tuple
   containing a key-value pair.  On failure, return *NULL*. This is equivalent to
Ezio Melotti's avatar
Ezio Melotti committed
67
   the Python expression ``list(o.items())``.
68 69


70
.. c:function:: PyObject* PyMapping_GetItemString(PyObject *o, const char *key)
71 72 73 74 75

   Return element of *o* corresponding to the object *key* or *NULL* on failure.
   This is the equivalent of the Python expression ``o[key]``.


76
.. c:function:: int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)
77 78 79

   Map the object *key* to the value *v* in object *o*. Returns ``-1`` on failure.
   This is the equivalent of the Python statement ``o[key] = v``.