[Python-ideas] weakref.WeakKeyDictionary is (basically) useless

Benjamin Peterson benjamin at python.org
Wed Dec 31 02:03:18 CET 2014


Kevin Norris <nykevin.norris at ...> writes:

> Why is it broken?  To go back to our simple example, imagine someone
> subclasses Bar (let's call the subclass Baz) and implements __eq__()
> and/or __hash__().  Instances of Baz may not work at all (if __hash__
> is None) or they may "work" but behave nonsensically (all values-equal
> instances of Baz will have the same .foo attribute - imagine trying to
> track that bug down!).  While we could forbid such a subclass in our
> API docs, this runs afoul of the open/closed principle.

Why not just use a wrapper like this for keys?

  class IdentityWrapper(object):
      def __init__(self, obj):
          self.obj = obj
      def __hash__(self):
          return id(self.obj)
      def __eq__(self, other):
          return self.obj is other






More information about the Python-ideas mailing list