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 1928700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:00:41+00:00 2026-05-17T07:00:41+00:00

In my code there’s numerous comparisons for equality of various containers (list, dict, etc.).

  • 0

In my code there’s numerous comparisons for equality of various containers (list, dict, etc.). The keys and values of the containers are of types float, bool, int, and str. The built-in == and != worked perfectly fine.

I just learned that the floats used in the values of the containers must be compared using a custom comparison function. I’ve written that function already (let’s call it approxEqual(), and assume that it takes two floats and return True if they are judged to be equal and False otherwise).

I prefer that the changes to the existing code are kept to a minimum. (New classes/functions/etc can be as complicated as necessary.)

Example:

if dict1 != dict2:
  raise DataMismatch

The dict1 != dict2 condition needs to be rewritten so that any floats used in values of dict1 and dict2 are compared using approxEqual function instead of __eq__.

The actual contents of dictionaries comes from various sources (parsing files, calculations, etc.).

Note: I asked a question earlier about how to override built-in float’s eq. That would have been an easy solution, but I learned that Python doesn’t allow overriding built-in types’ __eq__ operator. Hence this new question.

  • 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-17T07:00:41+00:00Added an answer on May 17, 2026 at 7:00 am

    The only route to altering the way built-in containers check equality is to make them contain as values, instead of the “originals”, wrapped values (wrapped in a class that overrides __eq__ and __ne__). This is if you need to alter the way the containers themselves use equality checking, e.g. for the purpose of the in operator where the right-hand side operand is a list — as well as in containers’ method such as their own __eq__ (type(x).__eq__(y) is the typical way Python will perform internally what you code as x == y).

    If what you’re talking about is performing your own equality checks (without altering the checks performed internally by the containers themselves), then the only way is to change every cont1 == cont2 into (e.g.) same(cont1, cont2, value_same) where value_same is a function accepting two values and returning True or False like == would. That’s probably too invasive WRT the criterion you specify.

    If you can change the container themselves (i.e., the number of places where container objects are created is much smaller than the number of places where two containers are checked for equality), then using a container subclass which overrides __eq__ is best.

    E.g.:

    class EqMixin(object):
      def __eq__(self, other):
        return same(cont1, cont2, value_same)
    

    (with same being as I mentioned in the A’s 2nd paragraph) and

    class EqM_list(EqMixin, list): pass
    

    (and so forth for other container types you need), then wherever you have (e.g.)

    x = list(someiter)
    

    change it into

    x = EqM_list(someiter)
    

    and be sure to also catch other ways to create list objects, e.g. replace

    x = [bah*2 for bah in buh]
    

    with

    x = EqM_list(bah*2 for bah in buh)
    

    and

    x = d.keys()
    

    with

    x = EqM_list(d.iterkeys())
    

    and so forth.

    Yeah, I know, what a bother — but it’s a core principle (and practice;-) of Python that builtin types (be they containers, or value types like e.g. float) themselves cannot be changed. That’s a very different philosophy from e.g. Ruby’s and Javascript’s (and I personally prefer it but I do see how it can seem limiting at times!).

    Edit: the OP specific request seems to be (in terms of this answer) “how do I implement same” for the various container types, not how to apply it without changing the == into a function call. If that’s correct, then (e.g) without using iterators for simplicity:

    def samelist(a, b, samevalue):
        if len(a) != len(b): return False
        return all(samevalue(x, y) for x, y in zip(a, b))
    
    def samedict(a, b, samevalue):
        if set(a) != set(b): return False
        return all(samevalue(a[x], b[x]) for x in a))
    

    Note that this applies to values, as requested, NOT to keys. “Fuzzying up” the equality comparison of a dict’s keys (or a set’s members) is a REAL problem. Look at it this way: first, how to you guarantee with absolute certainty that samevalue(a, b) and samevalue(b, c) totally implies and ensures samevalue(a, c)? This transitivity condition does not apply to most semi-sensible “fuzzy comparisons” I’ve ever seen, and yet it’s completely indispensable for the hash-table based containers (such as dicts and sets). If you pass that hurdle, then the nightmare of making the hash values somehow “magically” consistent arises — and what if two actually different keys in one dict “map to” equality in this sense with the same key in the other dict, which of the two corresponding values should be used then…? This way madness lies, if you ask me, so I hope that when you say values you do mean, exactly, values, and not keys!-)

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

Sidebar

Related Questions

In my code there are some calls to a method that set the values
I see in the code there is $(.add-to-list :select :selected) using jQuery and we
In Google Code there is a list of favorites. How do I add projects
If I code inline list items on separate lines of code there is a
In some existing code there is a test to see if the user is
In the code there exists exactly one type that implements IResourceConverter. That's what the
Sorry if this question seems trivial to many here. In a C++ code there
Is there code in VBA I can wrap a function with that will let
There is very little code out there that is in VB, and i'm getting
I'm refactoring some old JavaScript code and there's a lot of DOM manipulation going

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.