Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _dictobjects: |
| 4 | |
| 5 | Dictionary Objects |
| 6 | ------------------ |
| 7 | |
| 8 | .. index:: object: dictionary |
| 9 | |
| 10 | |
| 11 | .. ctype:: PyDictObject |
| 12 | |
| 13 | This subtype of :ctype:`PyObject` represents a Python dictionary object. |
| 14 | |
| 15 | |
| 16 | .. cvar:: PyTypeObject PyDict_Type |
| 17 | |
| 18 | .. index:: |
| 19 | single: DictType (in module types) |
| 20 | single: DictionaryType (in module types) |
| 21 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 22 | This instance of :ctype:`PyTypeObject` represents the Python dictionary |
| 23 | type. This is exposed to Python programs as ``dict`` and |
| 24 | ``types.DictType``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | .. cfunction:: int PyDict_Check(PyObject *p) |
| 28 | |
| 29 | Return true if *p* is a dict object or an instance of a subtype of the dict |
| 30 | type. |
| 31 | |
| 32 | |
| 33 | .. cfunction:: int PyDict_CheckExact(PyObject *p) |
| 34 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 35 | Return true if *p* is a dict object, but not an instance of a subtype of |
| 36 | the dict type. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | .. cfunction:: PyObject* PyDict_New() |
| 40 | |
| 41 | Return a new empty dictionary, or *NULL* on failure. |
| 42 | |
| 43 | |
| 44 | .. cfunction:: PyObject* PyDictProxy_New(PyObject *dict) |
| 45 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 46 | Return a proxy object for a mapping which enforces read-only behavior. |
| 47 | This is normally used to create a proxy to prevent modification of the |
| 48 | dictionary for non-dynamic class types. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | .. cfunction:: void PyDict_Clear(PyObject *p) |
| 52 | |
| 53 | Empty an existing dictionary of all key-value pairs. |
| 54 | |
| 55 | |
| 56 | .. cfunction:: int PyDict_Contains(PyObject *p, PyObject *key) |
| 57 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 58 | Determine if dictionary *p* contains *key*. If an item in *p* is matches |
| 59 | *key*, return ``1``, otherwise return ``0``. On error, return ``-1``. |
| 60 | This is equivalent to the Python expression ``key in p``. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | .. cfunction:: PyObject* PyDict_Copy(PyObject *p) |
| 64 | |
| 65 | Return a new dictionary that contains the same key-value pairs as *p*. |
| 66 | |
| 67 | |
| 68 | .. cfunction:: int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val) |
| 69 | |
| 70 | Insert *value* into the dictionary *p* with a key of *key*. *key* must be |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 71 | :term:`hashable`; if it isn't, :exc:`TypeError` will be raised. Return |
| 72 | ``0`` on success or ``-1`` on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | .. cfunction:: int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val) |
| 76 | |
Benjamin Peterson | 87d98bc | 2009-03-23 21:52:09 +0000 | [diff] [blame] | 77 | .. index:: single: PyUnicode_FromString() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 78 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 79 | Insert *value* into the dictionary *p* using *key* as a key. *key* should |
| 80 | be a :ctype:`char\*`. The key object is created using |
| 81 | ``PyUnicode_FromString(key)``. Return ``0`` on success or ``-1`` on |
Benjamin Peterson | 87d98bc | 2009-03-23 21:52:09 +0000 | [diff] [blame] | 82 | failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | .. cfunction:: int PyDict_DelItem(PyObject *p, PyObject *key) |
| 86 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 87 | Remove the entry in dictionary *p* with key *key*. *key* must be hashable; |
| 88 | if it isn't, :exc:`TypeError` is raised. Return ``0`` on success or ``-1`` |
| 89 | on failure. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | .. cfunction:: int PyDict_DelItemString(PyObject *p, char *key) |
| 93 | |
| 94 | Remove the entry in dictionary *p* which has a key specified by the string |
| 95 | *key*. Return ``0`` on success or ``-1`` on failure. |
| 96 | |
| 97 | |
| 98 | .. cfunction:: PyObject* PyDict_GetItem(PyObject *p, PyObject *key) |
| 99 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 100 | Return the object from dictionary *p* which has a key *key*. Return *NULL* |
| 101 | if the key *key* is not present, but *without* setting an exception. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
Alexandre Vassalotti | c3e36af | 2008-06-01 04:16:28 +0000 | [diff] [blame] | 103 | |
Alexandre Vassalotti | 787f307 | 2008-06-01 04:00:18 +0000 | [diff] [blame] | 104 | .. cfunction:: PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key) |
| 105 | |
| 106 | Variant of :cfunc:`PyDict_GetItem` that does not suppress |
| 107 | exceptions. Return *NULL* **with** an exception set if an exception |
| 108 | occurred. Return *NULL* **without** an exception set if the key |
| 109 | wasn't present. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 110 | |
Alexandre Vassalotti | c3e36af | 2008-06-01 04:16:28 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 112 | .. cfunction:: PyObject* PyDict_GetItemString(PyObject *p, const char *key) |
| 113 | |
| 114 | This is the same as :cfunc:`PyDict_GetItem`, but *key* is specified as a |
| 115 | :ctype:`char\*`, rather than a :ctype:`PyObject\*`. |
| 116 | |
| 117 | |
| 118 | .. cfunction:: PyObject* PyDict_Items(PyObject *p) |
| 119 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 120 | Return a :ctype:`PyListObject` containing all the items from the |
| 121 | dictionary, as in the dictionary method :meth:`dict.items`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | .. cfunction:: PyObject* PyDict_Keys(PyObject *p) |
| 125 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 126 | Return a :ctype:`PyListObject` containing all the keys from the dictionary, |
| 127 | as in the dictionary method :meth:`dict.keys`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | .. cfunction:: PyObject* PyDict_Values(PyObject *p) |
| 131 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 132 | Return a :ctype:`PyListObject` containing all the values from the |
| 133 | dictionary *p*, as in the dictionary method :meth:`dict.values`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | .. cfunction:: Py_ssize_t PyDict_Size(PyObject *p) |
| 137 | |
| 138 | .. index:: builtin: len |
| 139 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 140 | Return the number of items in the dictionary. This is equivalent to |
| 141 | ``len(p)`` on a dictionary. |
| 142 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 143 | |
| 144 | .. cfunction:: int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) |
| 145 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 146 | Iterate over all key-value pairs in the dictionary *p*. The |
| 147 | :ctype:`Py_ssize_t` referred to by *ppos* must be initialized to ``0`` |
| 148 | prior to the first call to this function to start the iteration; the |
| 149 | function returns true for each pair in the dictionary, and false once all |
| 150 | pairs have been reported. The parameters *pkey* and *pvalue* should either |
| 151 | point to :ctype:`PyObject\*` variables that will be filled in with each key |
| 152 | and value, respectively, or may be *NULL*. Any references returned through |
| 153 | them are borrowed. *ppos* should not be altered during iteration. Its |
| 154 | value represents offsets within the internal dictionary structure, and |
| 155 | since the structure is sparse, the offsets are not consecutive. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 156 | |
| 157 | For example:: |
| 158 | |
| 159 | PyObject *key, *value; |
| 160 | Py_ssize_t pos = 0; |
| 161 | |
| 162 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 163 | /* do something interesting with the values... */ |
| 164 | ... |
| 165 | } |
| 166 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 167 | The dictionary *p* should not be mutated during iteration. It is safe to |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 168 | modify the values of the keys as you iterate over the dictionary, but only |
| 169 | so long as the set of keys does not change. For example:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 170 | |
| 171 | PyObject *key, *value; |
| 172 | Py_ssize_t pos = 0; |
| 173 | |
| 174 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
| 175 | long i = PyLong_AsLong(value); |
| 176 | if (i == -1 && PyErr_Occurred()) { |
| 177 | return -1; |
| 178 | } |
| 179 | PyObject *o = PyLong_FromLong(i + 1); |
| 180 | if (o == NULL) |
| 181 | return -1; |
| 182 | if (PyDict_SetItem(self->dict, key, o) < 0) { |
| 183 | Py_DECREF(o); |
| 184 | return -1; |
| 185 | } |
| 186 | Py_DECREF(o); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | .. cfunction:: int PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 191 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 192 | Iterate over mapping object *b* adding key-value pairs to dictionary *a*. |
| 193 | *b* may be a dictionary, or any object supporting :func:`PyMapping_Keys` |
| 194 | and :func:`PyObject_GetItem`. If *override* is true, existing pairs in *a* |
| 195 | will be replaced if a matching key is found in *b*, otherwise pairs will |
| 196 | only be added if there is not a matching key in *a*. Return ``0`` on |
| 197 | success or ``-1`` if an exception was raised. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | .. cfunction:: int PyDict_Update(PyObject *a, PyObject *b) |
| 201 | |
| 202 | This is the same as ``PyDict_Merge(a, b, 1)`` in C, or ``a.update(b)`` in |
| 203 | Python. Return ``0`` on success or ``-1`` if an exception was raised. |
| 204 | |
| 205 | |
| 206 | .. cfunction:: int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override) |
| 207 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 208 | Update or merge into dictionary *a*, from the key-value pairs in *seq2*. |
| 209 | *seq2* must be an iterable object producing iterable objects of length 2, |
| 210 | viewed as key-value pairs. In case of duplicate keys, the last wins if |
| 211 | *override* is true, else the first wins. Return ``0`` on success or ``-1`` |
| 212 | if an exception was raised. Equivalent Python (except for the return |
| 213 | value):: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 214 | |
| 215 | def PyDict_MergeFromSeq2(a, seq2, override): |
| 216 | for key, value in seq2: |
| 217 | if override or key not in a: |
| 218 | a[key] = value |