I am wondering about how Python handles the user-defined objects. So here is the scenario:
I have created my own class called MyClass:
class MyClass():
def __init__(self):
# some code here
class MyClassContainer():
def __init__(self):
self.container = [] # will store MyClass object instances.
def Add(self, object):
self.container.append(object)
def Remove(self, object):
self.container.remove(object)
example = MyClassContainer()
myclass1 = MyClass()
example.Add(myclass1)
myclass2 = MyClass()
example.Add(myclass2)
example.Remove(myclass1)
So the question, is python's remove function able to distinguish different object instances of the same class? Is there any corner case that will not uniquely identify the object instance I wanted to delete?
A possible scenario is this one:
myclass1 = MyClass(5)
example.Add(myclass1)
myclass1 = MyClass(3)
example.Add(myclass1)
example.Remove(myclass1)
Which object instance will be removed? I guess the one with 3 passed, but is there a rule that says, python uniquely identify object instances of the same class?
__eq__. Basically locates themyclass1 == example.containerelement and removes that.