The `or` operator on dict.keys()
<p>As I've been unable to find any documentation on this, so I'll ask here. </p>

<p>As shown in the code below, I found that the <code>or</code> operator (<code>|</code>), worked as such:</p>

<pre><code>a = {"a": 1,"b": 2, 2: 3}
b = {"d": 10, "e": 11, 11: 12}

keys = a.keys() | b.keys()
aonce = a.keys() | a.values()
bonce = b.keys() | b.values()

for i in keys:
    print(i, end=" ")
print()
for i in aonce:
    print(i, end=" ")
print()
for i in bonce:
    print(i, end=" ")
print()
</code></pre>

<p>Which produces the result, in some order:</p>

<pre><code>2 d 11 a b e   
3 1 2 a b   
10 e 11 12 d   
</code></pre>

<p>Initially I assumed these iterable was compatible with <code>|</code>, similar to the way sets are, however. Testing with other iterable, such as a <code>list.__iter__()</code>, threw an error. Even;  </p>

<pre><code>values = a.values() | b.values()
for i in values:
    print(i, end=" ") 
print()
</code></pre>

<p>Which I'd assume worked, due to the use of <code>dict.values()</code> in the previous examples, threw an error.</p>

<p>So, my question is; What on earth have I come across, and more importantly, how reliable is it? What subclass does my arguments need to be, for me to be able to use this?</p>

<p>The <a href="https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects">Python 3 Documentation</a> notes that the <code>dict.keys</code> method is set-like and implements <a href="https://docs.python.org/3/library/collections.abc.html#collections.abc.Set"><code>collections.abc.Set</code></a>.</p>

<p>Note that <code>dict.values</code> is <strong>not set-like</strong> even though it might appear to be so in your examples:</p>

<pre><code>aonce = a.keys() | a.values()
bonce = b.keys() | b.values()
</code></pre>

<p>However these are leveraging off the fact that the keys view implements <code>__or__</code> (and <code>__ror__</code>) over arbitrary iterables.</p>

<p>For example, the following will not work:</p>

<pre><code>&gt;&gt;&gt; a.values() | b.values()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: unsupported operand type(s) for |: 'dict_values' and 'dict_values'
</code></pre>

python python-3.x python-3.5