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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:22:48+00:00 2026-06-03T10:22:48+00:00

When implementing the __eq__ and __lt__ methods of a class, it is common practice

  • 0

When implementing the __eq__ and __lt__ methods of a class, it is common practice to use tuples to group the values you wish to compare, like so:

@total_ordering
class Foo(object):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __hash__(self):
        return hash((self.c, self.a, self.b))

    def __eq__(self, other):
        return (self.c, self.a, self.b) == (other.c, other.a, other.b)

    def __lt__(self, other):
        return (self.c, self.a, self.b) < (other.c, other.a, other.b)

However this uses the natural ordering of each key. What if I want to change, for example, how a sorts?

This is what I’ve come up with so far, and while it seems to work OK, I was wondering if there is a better way to go about it:

@total_ordering
class Foo(object):
    def __init__(self, a, b, c):
        self.a = MyA(a) # Note
        self.b = b
        self.c = c

    def __hash__(self):
        return hash((self.c, self.a, self.b))

    def __eq__(self, other):
        return (self.c, self.a, self.b) == (other.c, other.a, other.b)

    def __lt__(self, other):
        return (self.c, self.a, self.b) < (other.c, other.a, other.b)

class MyA(A):
    def __hash__(self):
        # ...

    def __eq__(self, other):
        # ...

    def __lt__(self, other):
        # ...

Subclassing A lets me define my custom ordering, and allows MyA to behave like a regular A in every other way which is nice, but it seems wasteful / needlessly verbose especially if I have to do this for multiple fields.

Edit: As per user1320237’s answer below, this is what I’ve come up with:

@total_ordering
class Foo(object):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __hash__(self):
        return hash((self.c, self.a, self.b))

    def __eq__(self, other):
        return (0, 0, 0) == (cmp(self.c, other.c),
                             cmpA(self.a, other.a),
                             cmp(self.b, other.b))

    def __lt__(self, other):
        return (0, 0, 0) > (cmp(self.c, other.c),
                            cmpA(self.a, other.a),
                            cmp(self.b, other.b))

def cmpA(a1, a2):
    # ...

(Note the > in __lt__ since cmp(x, y) returns -1 if x < y and __lt__ should return True)

  • 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-06-03T10:22:50+00:00Added an answer on June 3, 2026 at 10:22 am

    if you want to order with list.sort() for example you can pass arguments to it:

    Your code:

    ...
        def __lt__(self, other):
            return (self.c, self.a, self.b) < (other.c, other.a, other.b)
    
    ...
    list.sort()
    

    equivalent:

    list.sort(key = lambda self: (self.c, self.a, self.b))
    

    also equivalent:

    list.sort(cmp = lambda self, other: \
                    (self.c, self.a, self.b) < (other.c, other.a, other.b))
    

    so if you want to sort your answers in different ways i would propose:

    class Foo(object):
        @staticmethod
        def cmp_absoluteOrder(self, other):
            return (self.c, self.a, self.b) < (other.c, other.a, other.b)
    
        @staticmethod
        def cmp_otherOrder(self, other):
            return ...
    
        @staticmethod
        def cmp_combinedSort(cmpA, cmpB, cmpC):
            return lambda self, other: (0, 0, 0) < (cmpA(self.c, other.c), cmpA(self.a, other.a), cmpA(self.b, other.b), )
    
        def __hash__(self):
            return hash(self.c) ^ hashA(self.a) ^ hash(self.b)
    
    ...
    list.sort(cmp = Foo.cmp_absoluteSorting)
    list.sort(cmp = Foo.cmp_combinedSort(cmp, (lambda a1, a2: ...), cmp))
    
    hashA = hash # or replace it if important # but the same a will retunrn the same hash
    

    or something like this

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

Sidebar

Related Questions

When implementing a class with multiple properties (like in the toy example below), what
When implementing a custom equality function for a class, does it make sense to
implementing service something similar with tinyurl or bit.ly, I'm would like to expose service
Implementing a custom VM and I've come to use registers (these will store pointers
Implementing a low pass FIR filter, when should one use FFT and IFFT instead
When implementing HTTP services in node.js, there is a lot of sample code like
When implementing move constructors and move assignment operators, one often writes code like this:
Im Implementing a video broadcasting website. For that I planned to use silverlight player
I'm implementing this: http://jsfiddle.net/xkfqN/85/ html: <div id=posts> <div class=more>Show More</div> <div class=commentsContainer> <div class=comment>Comment
Implementing a Thread by providing a new class that extends Thread and overriding its

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.