~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/c-api/dict.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>Dictionary Objects &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 2.7.11 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
 
    <link rel="up" title="Concrete Objects Layer" href="concrete.html" />
34
 
    <link rel="next" title="Class and Instance Objects" href="class.html" />
35
 
    <link rel="prev" title="List Objects" href="list.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="class.html" title="Class and Instance Objects"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="list.html" title="List Objects"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="dictionary-objects">
78
 
<span id="dictobjects"></span><h1>Dictionary Objects<a class="headerlink" href="#dictionary-objects" title="Permalink to this headline">¶</a></h1>
79
 
<span class="target" id="index-0"></span><dl class="type">
80
 
<dt id="c.PyDictObject">
81
 
<code class="descname">PyDictObject</code><a class="headerlink" href="#c.PyDictObject" title="Permalink to this definition">¶</a></dt>
82
 
<dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject</span></code></a> represents a Python dictionary object.</p>
83
 
</dd></dl>
84
 
 
85
 
<dl class="var">
86
 
<dt id="c.PyDict_Type">
87
 
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="descname">PyDict_Type</code><a class="headerlink" href="#c.PyDict_Type" title="Permalink to this definition">¶</a></dt>
88
 
<dd><p id="index-1">This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> represents the Python dictionary
89
 
type.  This is exposed to Python programs as <code class="docutils literal"><span class="pre">dict</span></code> and
90
 
<code class="docutils literal"><span class="pre">types.DictType</span></code>.</p>
91
 
</dd></dl>
92
 
 
93
 
<dl class="function">
94
 
<dt id="c.PyDict_Check">
95
 
int <code class="descname">PyDict_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Check" title="Permalink to this definition">¶</a></dt>
96
 
<dd><p>Return true if <em>p</em> is a dict object or an instance of a subtype of the dict
97
 
type.</p>
98
 
<div class="versionchanged">
99
 
<p><span class="versionmodified">Changed in version 2.2: </span>Allowed subtypes to be accepted.</p>
100
 
</div>
101
 
</dd></dl>
102
 
 
103
 
<dl class="function">
104
 
<dt id="c.PyDict_CheckExact">
105
 
int <code class="descname">PyDict_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_CheckExact" title="Permalink to this definition">¶</a></dt>
106
 
<dd><p>Return true if <em>p</em> is a dict object, but not an instance of a subtype of
107
 
the dict type.</p>
108
 
<div class="versionadded">
109
 
<p><span class="versionmodified">New in version 2.4.</span></p>
110
 
</div>
111
 
</dd></dl>
112
 
 
113
 
<dl class="function">
114
 
<dt id="c.PyDict_New">
115
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_New</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_New" title="Permalink to this definition">¶</a></dt>
116
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a new empty dictionary, or <em>NULL</em> on failure.</p>
117
 
</dd></dl>
118
 
 
119
 
<dl class="function">
120
 
<dt id="c.PyDictProxy_New">
121
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDictProxy_New</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*dict</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDictProxy_New" title="Permalink to this definition">¶</a></dt>
122
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a proxy object for a mapping which enforces read-only behavior.
123
 
This is normally used to create a proxy to prevent modification of the
124
 
dictionary for non-dynamic class types.</p>
125
 
<div class="versionadded">
126
 
<p><span class="versionmodified">New in version 2.2.</span></p>
127
 
</div>
128
 
</dd></dl>
129
 
 
130
 
<dl class="function">
131
 
<dt id="c.PyDict_Clear">
132
 
void <code class="descname">PyDict_Clear</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Clear" title="Permalink to this definition">¶</a></dt>
133
 
<dd><p>Empty an existing dictionary of all key-value pairs.</p>
134
 
</dd></dl>
135
 
 
136
 
<dl class="function">
137
 
<dt id="c.PyDict_Contains">
138
 
int <code class="descname">PyDict_Contains</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Contains" title="Permalink to this definition">¶</a></dt>
139
 
<dd><p>Determine if dictionary <em>p</em> contains <em>key</em>.  If an item in <em>p</em> is matches
140
 
<em>key</em>, return <code class="docutils literal"><span class="pre">1</span></code>, otherwise return <code class="docutils literal"><span class="pre">0</span></code>.  On error, return <code class="docutils literal"><span class="pre">-1</span></code>.
141
 
This is equivalent to the Python expression <code class="docutils literal"><span class="pre">key</span> <span class="pre">in</span> <span class="pre">p</span></code>.</p>
142
 
<div class="versionadded">
143
 
<p><span class="versionmodified">New in version 2.4.</span></p>
144
 
</div>
145
 
</dd></dl>
146
 
 
147
 
<dl class="function">
148
 
<dt id="c.PyDict_Copy">
149
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Copy</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Copy" title="Permalink to this definition">¶</a></dt>
150
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a new dictionary that contains the same key-value pairs as <em>p</em>.</p>
151
 
<div class="versionadded">
152
 
<p><span class="versionmodified">New in version 1.6.</span></p>
153
 
</div>
154
 
</dd></dl>
155
 
 
156
 
<dl class="function">
157
 
<dt id="c.PyDict_SetItem">
158
 
int <code class="descname">PyDict_SetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItem" title="Permalink to this definition">¶</a></dt>
159
 
<dd><p>Insert <em>value</em> into the dictionary <em>p</em> with a key of <em>key</em>.  <em>key</em> must be
160
 
<a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>; if it isn&#8217;t, <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> will be raised. Return
161
 
<code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on failure.</p>
162
 
</dd></dl>
163
 
 
164
 
<dl class="function">
165
 
<dt id="c.PyDict_SetItemString">
166
 
int <code class="descname">PyDict_SetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, const char<em>&nbsp;*key</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*val</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_SetItemString" title="Permalink to this definition">¶</a></dt>
167
 
<dd><p id="index-2">Insert <em>value</em> into the dictionary <em>p</em> using <em>key</em> as a key. <em>key</em> should
168
 
be a <code class="xref c c-type docutils literal"><span class="pre">char*</span></code>.  The key object is created using
169
 
<code class="docutils literal"><span class="pre">PyString_FromString(key)</span></code>.  Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on
170
 
failure.</p>
171
 
</dd></dl>
172
 
 
173
 
<dl class="function">
174
 
<dt id="c.PyDict_DelItem">
175
 
int <code class="descname">PyDict_DelItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItem" title="Permalink to this definition">¶</a></dt>
176
 
<dd><p>Remove the entry in dictionary <em>p</em> with key <em>key</em>. <em>key</em> must be hashable;
177
 
if it isn&#8217;t, <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> is raised.  Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code>
178
 
on failure.</p>
179
 
</dd></dl>
180
 
 
181
 
<dl class="function">
182
 
<dt id="c.PyDict_DelItemString">
183
 
int <code class="descname">PyDict_DelItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, char<em>&nbsp;*key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_DelItemString" title="Permalink to this definition">¶</a></dt>
184
 
<dd><p>Remove the entry in dictionary <em>p</em> which has a key specified by the string
185
 
<em>key</em>.  Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> on failure.</p>
186
 
</dd></dl>
187
 
 
188
 
<dl class="function">
189
 
<dt id="c.PyDict_GetItem">
190
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_GetItem</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItem" title="Permalink to this definition">¶</a></dt>
191
 
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Return the object from dictionary <em>p</em> which has a key <em>key</em>.  Return <em>NULL</em>
192
 
if the key <em>key</em> is not present, but <em>without</em> setting an exception.</p>
193
 
</dd></dl>
194
 
 
195
 
<dl class="function">
196
 
<dt id="c.PyDict_GetItemString">
197
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_GetItemString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, const char<em>&nbsp;*key</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_GetItemString" title="Permalink to this definition">¶</a></dt>
198
 
<dd><em class="refcount">Return value: Borrowed reference.</em><p>This is the same as <a class="reference internal" href="#c.PyDict_GetItem" title="PyDict_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PyDict_GetItem()</span></code></a>, but <em>key</em> is specified as a
199
 
<code class="xref c c-type docutils literal"><span class="pre">char*</span></code>, rather than a <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject*</span></code></a>.</p>
200
 
</dd></dl>
201
 
 
202
 
<dl class="function">
203
 
<dt id="c.PyDict_Items">
204
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Items</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Items" title="Permalink to this definition">¶</a></dt>
205
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the items from the
206
 
dictionary, as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal"><span class="pre">dict.items()</span></code></a>.</p>
207
 
</dd></dl>
208
 
 
209
 
<dl class="function">
210
 
<dt id="c.PyDict_Keys">
211
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Keys</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Keys" title="Permalink to this definition">¶</a></dt>
212
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the keys from the dictionary,
213
 
as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal"><span class="pre">dict.keys()</span></code></a>.</p>
214
 
</dd></dl>
215
 
 
216
 
<dl class="function">
217
 
<dt id="c.PyDict_Values">
218
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyDict_Values</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Values" title="Permalink to this definition">¶</a></dt>
219
 
<dd><em class="refcount">Return value: New reference.</em><p>Return a <a class="reference internal" href="list.html#c.PyListObject" title="PyListObject"><code class="xref c c-type docutils literal"><span class="pre">PyListObject</span></code></a> containing all the values from the
220
 
dictionary <em>p</em>, as in the dictionary method <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal"><span class="pre">dict.values()</span></code></a>.</p>
221
 
</dd></dl>
222
 
 
223
 
<dl class="function">
224
 
<dt id="c.PyDict_Size">
225
 
Py_ssize_t <code class="descname">PyDict_Size</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Size" title="Permalink to this definition">¶</a></dt>
226
 
<dd><p id="index-3">Return the number of items in the dictionary.  This is equivalent to
227
 
<code class="docutils literal"><span class="pre">len(p)</span></code> on a dictionary.</p>
228
 
<div class="versionchanged">
229
 
<p><span class="versionmodified">Changed in version 2.5: </span>This function returned an <code class="xref c c-type docutils literal"><span class="pre">int</span></code> type.  This might require changes
230
 
in your code for properly supporting 64-bit systems.</p>
231
 
</div>
232
 
</dd></dl>
233
 
 
234
 
<dl class="function">
235
 
<dt id="c.PyDict_Next">
236
 
int <code class="descname">PyDict_Next</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em>, Py_ssize_t<em>&nbsp;*ppos</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;**pkey</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;**pvalue</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Next" title="Permalink to this definition">¶</a></dt>
237
 
<dd><p>Iterate over all key-value pairs in the dictionary <em>p</em>.  The
238
 
<code class="xref c c-type docutils literal"><span class="pre">Py_ssize_t</span></code> referred to by <em>ppos</em> must be initialized to <code class="docutils literal"><span class="pre">0</span></code>
239
 
prior to the first call to this function to start the iteration; the
240
 
function returns true for each pair in the dictionary, and false once all
241
 
pairs have been reported.  The parameters <em>pkey</em> and <em>pvalue</em> should either
242
 
point to <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal"><span class="pre">PyObject*</span></code></a> variables that will be filled in with each key
243
 
and value, respectively, or may be <em>NULL</em>.  Any references returned through
244
 
them are borrowed.  <em>ppos</em> should not be altered during iteration. Its
245
 
value represents offsets within the internal dictionary structure, and
246
 
since the structure is sparse, the offsets are not consecutive.</p>
247
 
<p>For example:</p>
248
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
249
 
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
250
 
 
251
 
<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">key</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
252
 
    <span class="cm">/* do something interesting with the values... */</span>
253
 
    <span class="p">...</span>
254
 
<span class="p">}</span>
255
 
</pre></div>
256
 
</div>
257
 
<p>The dictionary <em>p</em> should not be mutated during iteration.  It is safe
258
 
(since Python 2.1) to modify the values of the keys as you iterate over the
259
 
dictionary, but only so long as the set of keys does not change.  For
260
 
example:</p>
261
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">PyObject</span> <span class="o">*</span><span class="n">key</span><span class="p">,</span> <span class="o">*</span><span class="n">value</span><span class="p">;</span>
262
 
<span class="n">Py_ssize_t</span> <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
263
 
 
264
 
<span class="k">while</span> <span class="p">(</span><span class="n">PyDict_Next</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">pos</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">key</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">value</span><span class="p">))</span> <span class="p">{</span>
265
 
    <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">PyInt_AS_LONG</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
266
 
    <span class="n">PyObject</span> <span class="o">*</span><span class="n">o</span> <span class="o">=</span> <span class="n">PyInt_FromLong</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
267
 
    <span class="k">if</span> <span class="p">(</span><span class="n">o</span> <span class="o">==</span> <span class="nb">NULL</span><span class="p">)</span>
268
 
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
269
 
    <span class="k">if</span> <span class="p">(</span><span class="n">PyDict_SetItem</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">dict</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">o</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
270
 
        <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
271
 
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
272
 
    <span class="p">}</span>
273
 
    <span class="n">Py_DECREF</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
274
 
<span class="p">}</span>
275
 
</pre></div>
276
 
</div>
277
 
<div class="versionchanged">
278
 
<p><span class="versionmodified">Changed in version 2.5: </span>This function used an <code class="xref c c-type docutils literal"><span class="pre">int</span> <span class="pre">*</span></code> type for <em>ppos</em>. This might require
279
 
changes in your code for properly supporting 64-bit systems.</p>
280
 
</div>
281
 
</dd></dl>
282
 
 
283
 
<dl class="function">
284
 
<dt id="c.PyDict_Merge">
285
 
int <code class="descname">PyDict_Merge</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*b</em>, int<em>&nbsp;override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Merge" title="Permalink to this definition">¶</a></dt>
286
 
<dd><p>Iterate over mapping object <em>b</em> adding key-value pairs to dictionary <em>a</em>.
287
 
<em>b</em> may be a dictionary, or any object supporting <a class="reference internal" href="mapping.html#c.PyMapping_Keys" title="PyMapping_Keys"><code class="xref c c-func docutils literal"><span class="pre">PyMapping_Keys()</span></code></a>
288
 
and <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal"><span class="pre">PyObject_GetItem()</span></code></a>. If <em>override</em> is true, existing pairs in <em>a</em>
289
 
will be replaced if a matching key is found in <em>b</em>, otherwise pairs will
290
 
only be added if there is not a matching key in <em>a</em>. Return <code class="docutils literal"><span class="pre">0</span></code> on
291
 
success or <code class="docutils literal"><span class="pre">-1</span></code> if an exception was raised.</p>
292
 
<div class="versionadded">
293
 
<p><span class="versionmodified">New in version 2.2.</span></p>
294
 
</div>
295
 
</dd></dl>
296
 
 
297
 
<dl class="function">
298
 
<dt id="c.PyDict_Update">
299
 
int <code class="descname">PyDict_Update</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*b</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_Update" title="Permalink to this definition">¶</a></dt>
300
 
<dd><p>This is the same as <code class="docutils literal"><span class="pre">PyDict_Merge(a,</span> <span class="pre">b,</span> <span class="pre">1)</span></code> in C, and is similar to
301
 
<code class="docutils literal"><span class="pre">a.update(b)</span></code> in Python except that <a class="reference internal" href="#c.PyDict_Update" title="PyDict_Update"><code class="xref c c-func docutils literal"><span class="pre">PyDict_Update()</span></code></a> doesn&#8217;t fall
302
 
back to the iterating over a sequence of key value pairs if the second
303
 
argument has no &#8220;keys&#8221; attribute.  Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code> if an
304
 
exception was raised.</p>
305
 
<div class="versionadded">
306
 
<p><span class="versionmodified">New in version 2.2.</span></p>
307
 
</div>
308
 
</dd></dl>
309
 
 
310
 
<dl class="function">
311
 
<dt id="c.PyDict_MergeFromSeq2">
312
 
int <code class="descname">PyDict_MergeFromSeq2</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*a</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*seq2</em>, int<em>&nbsp;override</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDict_MergeFromSeq2" title="Permalink to this definition">¶</a></dt>
313
 
<dd><p>Update or merge into dictionary <em>a</em>, from the key-value pairs in <em>seq2</em>.
314
 
<em>seq2</em> must be an iterable object producing iterable objects of length 2,
315
 
viewed as key-value pairs.  In case of duplicate keys, the last wins if
316
 
<em>override</em> is true, else the first wins. Return <code class="docutils literal"><span class="pre">0</span></code> on success or <code class="docutils literal"><span class="pre">-1</span></code>
317
 
if an exception was raised. Equivalent Python (except for the return
318
 
value):</p>
319
 
<div class="highlight-c"><div class="highlight"><pre><span class="n">def</span> <span class="n">PyDict_MergeFromSeq2</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">seq2</span><span class="p">,</span> <span class="n">override</span><span class="p">)</span><span class="o">:</span>
320
 
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="n">in</span> <span class="nl">seq2</span><span class="p">:</span>
321
 
        <span class="k">if</span> <span class="n">override</span> <span class="n">or</span> <span class="n">key</span> <span class="n">not</span> <span class="n">in</span> <span class="nl">a</span><span class="p">:</span>
322
 
            <span class="n">a</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
323
 
</pre></div>
324
 
</div>
325
 
<div class="versionadded">
326
 
<p><span class="versionmodified">New in version 2.2.</span></p>
327
 
</div>
328
 
</dd></dl>
329
 
 
330
 
</div>
331
 
 
332
 
 
333
 
          </div>
334
 
        </div>
335
 
      </div>
336
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
337
 
        <div class="sphinxsidebarwrapper">
338
 
  <h4>Previous topic</h4>
339
 
  <p class="topless"><a href="list.html"
340
 
                        title="previous chapter">List Objects</a></p>
341
 
  <h4>Next topic</h4>
342
 
  <p class="topless"><a href="class.html"
343
 
                        title="next chapter">Class and Instance Objects</a></p>
344
 
<h3>This Page</h3>
345
 
<ul class="this-page-menu">
346
 
  <li><a href="../bugs.html">Report a Bug</a></li>
347
 
  <li><a href="../_sources/c-api/dict.txt"
348
 
         rel="nofollow">Show Source</a></li>
349
 
</ul>
350
 
 
351
 
<div id="searchbox" style="display: none" role="search">
352
 
  <h3>Quick search</h3>
353
 
    <form class="search" action="../search.html" method="get">
354
 
      <input type="text" name="q" />
355
 
      <input type="submit" value="Go" />
356
 
      <input type="hidden" name="check_keywords" value="yes" />
357
 
      <input type="hidden" name="area" value="default" />
358
 
    </form>
359
 
    <p class="searchtip" style="font-size: 90%">
360
 
    Enter search terms or a module, class or function name.
361
 
    </p>
362
 
</div>
363
 
<script type="text/javascript">$('#searchbox').show(0);</script>
364
 
        </div>
365
 
      </div>
366
 
      <div class="clearer"></div>
367
 
    </div>  
368
 
    <div class="related" role="navigation" aria-label="related navigation">
369
 
      <h3>Navigation</h3>
370
 
      <ul>
371
 
        <li class="right" style="margin-right: 10px">
372
 
          <a href="../genindex.html" title="General Index"
373
 
             >index</a></li>
374
 
        <li class="right" >
375
 
          <a href="../py-modindex.html" title="Python Module Index"
376
 
             >modules</a> |</li>
377
 
        <li class="right" >
378
 
          <a href="class.html" title="Class and Instance Objects"
379
 
             >next</a> |</li>
380
 
        <li class="right" >
381
 
          <a href="list.html" title="List Objects"
382
 
             >previous</a> |</li>
383
 
        <li><img src="../_static/py.png" alt=""
384
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
385
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
386
 
        <li>
387
 
          <span class="version_switcher_placeholder">2.7.11</span>
388
 
          <a href="../index.html">Documentation</a> &raquo;
389
 
        </li>
390
 
 
391
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
392
 
          <li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> &raquo;</li> 
393
 
      </ul>
394
 
    </div>  
395
 
    <div class="footer">
396
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
397
 
    <br />
398
 
    The Python Software Foundation is a non-profit corporation.
399
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
400
 
    <br />
401
 
    Last updated on Jan 23, 2016.
402
 
    <a href="../bugs.html">Found a bug</a>?
403
 
    <br />
404
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
405
 
    </div>
406
 
 
407
 
  </body>
408
 
</html>
 
 
b'\\ No newline at end of file'