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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:51:48+00:00 2026-05-15T22:51:48+00:00

In java, I have my client class that have the code attr, and the

  • 0

In java, I have my client class that have the “code” attr, and the equals method. Method equals receives another client and compares with itself’s code attr.

In python, I just read that we have the __cmp__ method, to do the same as java method equals. Ok, I did that. I created my class client, with “code” attr and the method comp that verify if the code is the same.

class Client():
    def __init__(self, code):
        self.code = code

    def __cmp__(self, obj):
        return obj.code == self.code

    def __repr__(self):
        return str(self.code)

Then I put 3 Client objects in a python’s list:

bla = [Client(1), Client(2), Client(3)]

Then, when I try:

bla.remove(Client(3))

The amazing python removes the first element (the Client with code 1).

What I am doing wrong? I searched the implementation of list in python’s Lib files, but is not easy to find.

Anyone can help?

  • 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-15T22:51:49+00:00Added an answer on May 15, 2026 at 10:51 pm

    http://docs.python.org/reference/datamodel.html#object.__cmp__

    __cmp__(self, other)

    Called by comparison operations if
    rich comparison (see above) is not
    defined. Should return a negative
    integer if self < other, zero if self
    == other, a positive integer if self > other.

    Basically, you should change your implementation of __cmp__ to be…

    def __cmp__(self, obj):
        return cmp(obj.code, self.code)
    

    The cmp() builtin function of Python is specifically designed to return the values that __cmp__ is expected to return by comparing its two arguments.

    There is also a different function in Python called __eq__ which only checks equality, for which your current implementation of __cmp__ would be better suited.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In answer to the first question, I'd recommend PHPExcel, although… May 16, 2026 at 3:40 am
  • Editorial Team
    Editorial Team added an answer Based on this post i'd say it can't be done.… May 16, 2026 at 3:40 am
  • Editorial Team
    Editorial Team added an answer Just change it to: if (p.enumValue == theEnum.VAL1) { }… May 16, 2026 at 3:40 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

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.