Вы можете использовать оператор is
для операции, он проверяет идентичность, что объект является тем же объектом, а не эквилийтом:
Код может выглядеть следующим образом:
def transfer(self, source, destination):
for i in range(len(source)): # check all elements of the list
# check if element is self ("is" checks not equality but identity)
# "==" checks identity
if source[i] is self:
element = source.pop(i) # remove element by index to be sure that it is what we need
break
destination.append(element)
Некоторые тесты:
def transfer(x, source, destination):
for i in range(len(source)): # check all elements of the list
# check if element is self ("is" checks not equality but identity)
# "==" checks identity
print(source[i]) # will print 4 times to prove that 4th element was taken
if source[i] is x:
element = source.pop(i) # remove element by index to be sure that it is what we need
break
destination.append(element)
class Test:
def __init__(self, x):
self.x = x
def __eq__(self, other):
if self.x == other.x:
return True
return False
def __repr__(self):
return "Test({})".format(self.x)
x, y, z, a = Test(1), Test(2), Test(3), Test(3)
list_1 = [x, y, z, a]
list_2 = []
transfer(a, list_1, list_2)
print(list_1)
print(list_2)
Дайте мне знать, если это помогло, не стесняйтесь задавать вопросы.