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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:54+00:00 2026-05-27T15:01:54+00:00

class A(object): def __init__(self, value): self.value = value x = A(1) y = A(2)

  • 0
class A(object):

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

x = A(1)
y = A(2)

q = [x, y]
q.remove(y)

I want to remove from the list a specific object which was added before to it and to which I still have a reference. I do not want an equality test. I want an identity test. This code seems to work in both CPython and IronPython, but does the language guarantee this behavior or is it just a fluke?

The list.remove method documentation is this: same as del s[s.index(x)], which implies that an equality test is performed.

So will an object be equal to itself if you don’t override __cmp__, __eq__ or __ne__?

  • 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-27T15:01:55+00:00Added an answer on May 27, 2026 at 3:01 pm

    Yes. In your example q.remove(y) would remove the first occurrence of an object which compares equal with y. However, the way the class A is defined, you shouldn’t† ever have a variable compare equal with y – with the exception of any other names which are also bound to the same y instance.

    The relevant section of the docs is here:

    If no __cmp__(), __eq__() or __ne__() operation is defined, class
    instances are compared by object identity (“address”).

    So comparison for A instances is by identity (implemented as memory address in CPython). No other object can have an identity equal to id(y) within y‘s lifetime, i.e. for as long as you hold a reference to y (which you must, if you’re going to remove it from a list!)

    † Technically, it is still possible to have objects at other memory locations which are comparing equal – mock.ANY is one such example. But these objects need to override their comparison operators to force the result.

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

Sidebar

Related Questions

class Node(object): def __init__(self, lst): if type(lst) == list: self.value = lst[0] self.children =
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
I have a list of many Python objects like this: class RangeClass(object): def __init__(self,address,size):
class MyClass(object): def __init__(self): self._my_secret_thing = 1 def _i_get(self): return self._my_secret_thing def _i_set(self, value):
I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
What I am trying to achieve is something like this: class object: def __init__(self):
Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var
I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have a class like the following: class Positive(object): def __init__(self, item): self._validate_item(item) self.item

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.