What can you do with a PyDictValues_Type?
<p>While trying to convert some Python/C API code to work in both 2 &amp; 3, I found that, given the following Python</p>

<pre><code>DICT = { â¦ }

class Example(object):
  ITEMS = DICT.values()
</code></pre>

<p>and then calling <code>PyObject_GetAttrString(an_example, "ITEMS")</code> would yield a <code>PyObject</code> for which <code>PySequence_Check</code> would return true in 2.7. Now in 3.4, it's yielding a <code>PyObject</code> whose type is <code>PyDictValues_Type</code> and <code>PySequence_Check</code> doesn't return true. The documentation on <code>PyDictValues</code> is, ahem, sparse. What can one do with it? It also response false to <code>PyIter_Check</code>.</p>

<p>Basically just <code>iter</code> (<code>PyObject_GetIter</code> in the C API).</p>

<p>There are technically other operations, like <code>==</code> (inherited from <code>object</code>, uninteresting), <code>len</code> (but you'd call that on the dict instead of making a values view if you wanted that), and <code>in</code> (slow, linear scan, avoid it), but the primary use of a values view is as an iterable.</p>

<p>If you want a list, like Python 2, using the C API call <code>PyDict_Values</code> on the dict instead of calling the <code>values</code> method still makes a list.</p>

<p>Keys and items views (<code>keys()</code> and <code>items()</code>) are more interesting. They support all the operations in the <a href="https://docs.python.org/3/library/collections.abc.html#module-collections.abc" rel="nofollow"><code>collections.abc.Set</code></a> interface, which you'd access through the usual C API equivalents of those operations (so things like <code>PyObject_RichCompare</code> or <code>PySequence_Contains</code>, even though they're not sequences).</p>

python python-2.7 python-3.4 python-c-api