Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:48:47+00:00 2026-05-27T10:48:47+00:00

I have subclassed dict to add an extra method (so no overriding). Now, I

  • 0

I have subclassed dict to add an extra method (so no overriding).

Now, I try to compare two of those subclasses, and I get something weird :

>>> d1.items() == d2.items()
True
>>> d1.values() == d2.values()
True
>>> d1.keys() == d2.keys()
True
>>> d1 == d2
False

EDIT

That’s damn weird … I don’t understand at all ! Anybody with an insight on how the dict.eq is implemented ?

Following is all the code :

# ------ Bellow is my dict subclass (with no overriding) :

class ClassSetDict(dict):

    def subsetget(self, klass, default=None):
        class_sets = set(filter(lambda cs: klass <= cs, self))
        # Eliminate supersets
        for cs1 in class_sets.copy():
            for cs2 in class_sets.copy():
                if cs1 <= cs2 and not cs1 is cs2:
                    class_sets.discard(cs2)
        try:
            best_match = list(class_sets)[0]
        except IndexError:
            return default
        return self[best_match]

# ------  Then an implementation of class sets

class ClassSet(object):
    # Set of classes, allowing to easily calculate inclusions
    # with comparison operators : `a < B` <=> "A strictly included in B"

    def __init__(self, klass):
        self.klass = klass

    def __ne__(self, other):
        return not self == other

    def __gt__(self, other):
        other = self._default_to_singleton(other)
        return not self == other and other < self

    def __le__(self, other):
        return self < other or self == other

    def __ge__(self, other):
        return self > other or self == other

    def _default_to_singleton(self, klass):
        if not isinstance(klass, ClassSet):
            return Singleton(klass)
        else:
            return klass


class Singleton(ClassSet):

    def __eq__(self, other):
        other = self._default_to_singleton(other)
        return self.klass == other.klass

    def __lt__(self, other):
        if isinstance(other, AllSubSetsOf):
            return issubclass(self.klass, other.klass)
        else:
            return False


class AllSubSetsOf(ClassSet):

    def __eq__(self, other):
        if isinstance(other, AllSubSetsOf):
            return self.klass == other.klass
        else:
            return False

    def __lt__(self, other):
        if isinstance(other, AllSubSetsOf):
            return issubclass(self.klass, other.klass) and not other == self
        else:
            return False

# ------ and finally the 2 dicts that don't want to be equal !!!

d1 = ClassSetDict({AllSubSetsOf(object): (int,)})
d2 = ClassSetDict({AllSubSetsOf(object): (int,)})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:48:48+00:00Added an answer on May 27, 2026 at 10:48 am

    the problem you’re seing has nothing at all to do with subclassing dict. in fact this behavior can be seen using a regular dict. The problem is how you have defined the keys you’re using. A simple class like:

    >>> class Foo(object):
    ...     def __init__(self, value):
    ...         self.value = value
    ... 
    ...     def __eq__(self, other):
    ...         return self.value == other.value
    ... 
    

    Is enough to demonstrate the problem:

    >>> f1 = Foo(5)
    >>> f2 = Foo(5)
    >>> f1 == f2
    True
    >>> d1 = {f1: 6}
    >>> d2 = {f2: 6}
    >>> d1.items() == d2.items()
    True
    >>> d1 == d2
    False
    

    What’s missing is that you forgot to define __hash__. Every time you change the equality semantics of a class, you should make sure that the __hash__ method agrees with it: when two objects are equal, they must have equal hashes. dict behavior depends strongly on the hash value of keys.

    When you inherit from object, you automatically get both __eq__ and __hash__, the former compares object identity, and the latter returns the address of the object (so they agree), but when you change __eq__, you’re still seeing the old __hash__, which no longer agrees and dict gets lost.

    Simply provide a __hash__ method that in a stable way combines the hash values of its attributes.

    >>> class Bar(object):
    ...     def __init__(self, value):
    ...         self.value = value
    ... 
    ...     def __eq__(self, other):
    ...         return self.value == other.value
    ... 
    ...     def __hash__(self):
    ...         return hash((Bar, self.value))
    ... 
    >>> b1 = Bar(5)
    >>> b2 = Bar(5)
    >>> {b1: 6} == {b2: 6}
    True
    >>> 
    

    When using __hash__ in this way, it’s also a good idea to make sure that the attributes do not (or better, cannot) change after the object is created. If the hash value changes while collected in a dict, the key will be “lost”, and all sorts of weird things can happen (even weirder than the issue you initially asked about)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Counter objects are subclasses of dict so they have the method setdefault. >>> from
I have subclassed Form to include some extra functionality, which boils down to a
I have subclassed java.awt.Frame and have overridden the paint() method as I wish to
We have subclassed the Silverlight Application class to add some additional functionality and then
I have subclassed a UITableViewCell and within this class I want to get it's
Currenly in my cellForRowAtIndexPath function i have subclassed two UILabels that holds the values
I have subclassed UITableViewCell to make a custom cell for a tableView. I add
I have a subclassed NSTextField to limit the maximum length (only 1 character). Now,
I have a UITableViewSource which I have subclassed. I'm overriding GetCell and using my
I have subclass that inherits from a dict. On __getitem__ method I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.