[Python-Dev] PEP 372 -- Adding an ordered directory to collectionsready for pronouncement

Guido van Rossum guido at python.org
Mon Mar 2 19:27:16 CET 2009


On Mon, Mar 2, 2009 at 9:39 AM, Raymond Hettinger <python at rcn.com> wrote:
>
> [Antoine Pitrou]
>>
>> You seem to imply that it is more important for __eq__ to work intuitively
>> between a non-OrderedDict and an OrderedDict, than it is to work
>> intuitively
>> between two OrderedDicts.
>
> Yes.  When Armin and I worked through this, it became clear that
> he had multiple use cases where ordered dicts needed to be used
> in places that had been originally designed to expect regular dicts.
> That was also the reason for subclassing dict.  Otherwise, we would
> have just made a standalone class that defined all the mapping methods.
>
> I don't think we going to convince you and that's okay.  We don't
> have to agree on every design decision.  There were some reasons
> for either approach and we picked the one that best fit Armin's use
> cases, that was simplest, that introduced the fewest special rules,
> and did not create a Liskov violation.  The choice was clearly
> documented and an alternative was provided for people that
> needed it.

But you'll have to convince me, and so far I agree with Antoine that
doing the comparison without taking the order into account feels
really weird. I also think that comparing an odict to a dict with the
same items and expecting them to be the same feels wrong. It is not
needed to invoke Liskov: Liskov cares about the signature, not about
the computed value. There is no rule that says you are not allowed to
override odict.__eq__ so that it returns False in cases where
Mapping.__eq__ returns True.

I would propose the following formal specification for odict.__eq__:

  def __eq__(self, other):
    if not isinstance(other, odict):
      return NotImplemented  # Give other a chance; defaults to False
    return list(self.items()) == list(other.items())

Obviously an actual implementation can do something more complex
instead of the last line, like:

  for a, b in zip(self.items(), other.items()):
    if a != b:
      return False
  return True

> Outside of your differing judgment on the __eq__ method, are you
> basically happy with the ordered dict PEP?

I am.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list