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

  • Home
  • SEARCH
  • 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 960519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:11:40+00:00 2026-05-16T01:11:40+00:00

How can i change the __cmp__ function of an instance (not in class)? Ex:

  • 0

How can i change the __cmp__ function of an instance (not in class)?

Ex:

class foo:
    def __init__(self, num):
        self.num = num

def cmp(self, other):
    return self.num - other.num

# Change __cmp__ function in class works
foo.__cmp__ = cmp
a = foo(1)
b = foo(1)

# returns True
a == b



# Change __cmp__ function in instance that way doesnt work
def cmp2(self, other):
    return -1

a.__cmp__ = cmp2
b.__cmp__ = cmp2

# Raise error 
a == b
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#TypeError: cmp2() takes exactly 2 arguments (1 given)
  • 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-16T01:11:41+00:00Added an answer on May 16, 2026 at 1:11 am

    DO NOT DO THIS

    It will make your code buggy and hard to maintain. The reason it is difficult is because the right way to do it is to subclass foo:

    class FunkyCmpFoo( foo ):
        def __cmp__( self, other ):
            return -1
    

    &c., &c. This way, you know that all foos compare in the same way, and all FunkyCmpFoos compare in the same way. If you don’t, you will eventually end up comparing a modified foo with an original foo, and Cthulhu himself will rise from the depths to punish you.

    I’m not sure whether I should say this, but it is possible, by creating your own instance methods:

    funcType = type( foo.__cmp__ )
    # Alternatively:
    import new
    cmp2 = new.instancemethod( func, a, foo )
    
    a.__cmp__ = funcType( cmp2, a, foo )
    b.__cmp__ = funcType( cmp2, b, foo )
    

    I can think of one good reason to do this, and that is if your archenemy has to debug the code. In fact, I can think of some quite fun things to do with that in mind (how would you like sys.maxint to compare less than all even numbers?). Apart from that, it’s a nightmare.

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

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess, here is the answer: https://bugs.eclipse.org/bugs/show_bug.cgi?id=319514 May 16, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer No, you should start learning 8086 ASM by learning 8086… May 16, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer You're actually overthinking this -- the "market.android.com/??" urls never even… May 16, 2026 at 11:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

we can change the background of buttons and menu items n other UI components
I'm trying to change pageindex on page_Load event but its not working. I can
You can change the connection string at run-time like this. You make the connection
I can change prompt_alternatives_on flag in the REPL. But how do I change this
I can change the icon in the WindowsForm. How can I actually change the
I can change the width of an upload field with the size attribute: <input
As I've found here , I can change the webconfig to allow/disallow methods on
I have encountered some strange Perl behavior: using a Posix character class in a
how can I edit the taxonomy view ? I'm talking about the view that
HI All, I need to change the iphone ringtone and wallpaper programmatic by my

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.