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