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>In Python 2, <code>dict.values()</code> returns a list, but in Python 3, it's a view of dictionary values. The equivalent in Python 2.7 is <a href="https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects" rel="nofollow"><code>dict.viewvalues()</code></a>.</p>

<p>In particular views are not sequences because dictionaries don't have orders. So <code>dict.viewkeys()</code> and <code>dict.viewitems()</code> are set-like, because keys have to be unique. <code>dict.viewvalues()</code> is multi-set-like. You can iterate over it, get the length, and do contains checks against it. But you can't index into it like a sequence.</p>

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