<div dir="ltr">* DOC: OrderDict.values() comparisons in Python 3<div><div>  * Src: <a href="https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#l793">https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#l793</a><br></div></div><div><br></div><div>What should it say?</div><div><br></div><div><div>```rst</div><div><br></div><div>.. `<<a href="https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#l793">https://hg.python.org/cpython/file/f2a0a4a45292/Doc/library/collections.rst#l793</a>>`__</div><div><br></div><div>collections.OrderedDict.values().__eq__</div><div><br></div><div>* "is suprising"</div><div>* Python 3 has `dict views`_</div><div><br></div><div>* :class:`OrderedDict` matches the dict interface in Python 2.7 and</div><div>  Python 3.</div><div>  * <a href="https://docs.python.org/2/library/collections.html#collections.OrderedDict">https://docs.python.org/2/library/collections.html#collections.OrderedDict</a></div><div>  * <a href="https://docs.python.org/3/library/collections.html#collections.OrderedDict">https://docs.python.org/3/library/collections.html#collections.OrderedDict</a></div><div><br></div><div>* Python 2 dict interface:</div><div>  * dict.viewkeys(), dict.viewvalues(), dict.viewitems()</div><div>  * dict.keys(), dict.values(), dict.items()</div><div>  * <a href="https://docs.python.org/2/library/stdtypes.html#dict">https://docs.python.org/2/library/stdtypes.html#dict</a></div><div>  * <a href="https://docs.python.org/2/library/stdtypes.html#dict.values">https://docs.python.org/2/library/stdtypes.html#dict.values</a></div><div>  * <a href="https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects">https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects</a></div><div>* Python 3 dict interface (:ref:`dictionary view objects`:</div><div>  * dict.keys(), dict.values(), dict.items()</div><div>  * list(dict.keys()), list(dict.values()), list(dict.items())</div><div>  * <a href="https://docs.python.org/3/library/stdtypes.html#dict">https://docs.python.org/3/library/stdtypes.html#dict</a></div><div>  * <a href="https://docs.python.org/3/library/stdtypes.html#dict.values">https://docs.python.org/3/library/stdtypes.html#dict.values</a></div><div>  * <a href="https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects">https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects</a></div><div><br></div><div><br></div><div>* In order to compare OrderedDict.values() **by value**</div><div>  you must either:</div><div>  </div><div>  * Cast values() to a sequence (e.g. a list) before comparison</div><div>  * Subclass :class:`OrderedDict` and wrap `values()`</div><div><br></div><div><br></div><div>.. code:: python</div><div><br></div><div>    from collections import OrderedDict</div><div>    a = 'a'</div><div>    x = 1</div><div>    y = x</div><div>    ab  =  [( a,  x), ('b', 2)]</div><div>    ba  =  [('b', 2),  (a,  y)]</div><div>    ab_odict      = OrderedDict(ab)</div><div>    ab_odict_     = OrderedDict(ab)</div><div>    ab_odict_copy = OrderedDict(ab.copy())</div><div>    ba_odict      = OrderedDict(ba)</div><div>    ab_dict = dict(ab)</div><div>    ba_dict = dict(ba)</div><div><br></div><div>    # In Python 3,</div><div>    # OrderedDict.values.__eq__ does not compare by value:</div><div>    assert (     ab_odict.values()  ==      ab_odict_.values()) is False</div><div>    assert (list(ab_odict.values()) == list(ab_odict_.values()) is True</div><div><br></div><div>    # In Python 2.7 and 3,</div><div>    # OrderedDict.__eq__ compares ordered sequences</div><div>    assert (ab_odict == ab_odict_)     is True</div><div>    assert (ab_odict == ab_odict_copy) is True</div><div>    assert (ab_odict == ba_odict) is False</div><div>    assert (ab_dict  == ba_dict)  is True</div><div>    </div><div><br></div><div>    # - [ ] How to explain the x, y part?</div><div>    #   - in terms of references, __eq__, id(obj), __hash__</div><div><br></div></div><div>```</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 20, 2016 at 12:39 PM, Sven R. Kunze <span dir="ltr"><<a href="mailto:srkunze@mail.de" target="_blank">srkunze@mail.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Documentation is a very good idea.<br>
    <br>
    Maybe, even raise an error when comparing values.<br>
    <br>
    Best,<br>
    Sven<div><div class="h5"><br>
    <br>
    <div>On 20.01.2016 12:13, Alexandre Figura
      wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr">
        <div>
          <div>
            <div>If we put technical considerations aside, maybe we
              should just ask to ourselves what behavior do we expect
              when doing equality tests between ordered dictionaries. As
              a reminder:<br>
              <br>
              >>> xy = OrderedDict([('x', None), ('y', None)])<br>
              >>> yx = OrderedDict([('y', None), ('x', None)])<br>
              >>> xy == yx<br>
              False<br>
              >>> xy.items() == yx.items()<br>
              True<br>
              >>> xy.keys() == yx.keys()<br>
              True<br>
              >>> xy.values() == yx.values()<br>
              False<br>
              <br>
              So, it appears that:<br>
              1. equality tests between odict_values use objects
              identity and not equality,<br>
            </div>
            2. equality tests between odict_keys do not respect order.<br>
            <br>
          </div>
          If it is not technically possible to change the current
          implementation, maybe all we can do is just add a warning
          about current behavior in the documentation?<br>
        </div>
        <div class="gmail_extra"><br>
          <div class="gmail_quote">On Mon, Jan 11, 2016 at 4:17 AM,
            Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank"></a><a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br>
            <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
              <div dir="ltr">Seems like we dropped the ball... Is there
                any action item here?<span><br clear="all">
                  <div class="gmail_extra"><br>
                    -- <br>
                    <div>--Guido van Rossum (<a href="http://python.org/%7Eguido" target="_blank">python.org/~guido</a>)</div>
                  </div>
                </span></div>
              <br>
              _______________________________________________<br>
              Python-ideas mailing list<br>
              <a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
              <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
              Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
            </blockquote>
          </div>
          <br>
        </div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      <pre>_______________________________________________
Python-ideas mailing list
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a></pre>
    </blockquote>
    <br>
  </div></div></div>

<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br></div>