I have 2 instances x and y of a same class RBnode.
Is there a way to exchange their identities so that all reference to x goes to y and vice versa?
For example,
x = RBnode()
y = RBnode()
x.data = 1
y.data = 2
L = [x,y]
exchange_identity(x,y)
print x.data, y.data, (L[0] is y)
>>> 2 1 True
Actually I’m building an extension of Red-Black tree. Nodes of the tree are implemented as object. When writing the node removal method, I need to exchange two nodes, so that the node to be removed gets “at the bottom” of the tree.
First I tried to just exchange data in the two nodes:
def remove_node(self, y):
''' remove node y from tree
return (y.key,y.value) if successful'''
... ... ...
# exchange y's data with that of its successor y.next
y.key = y.next.key
y.value = y.next.value
But later an error occurred when remove_node was called and the caller function was holding a reference to a node x, which happens to be y.next. Something like
x = y.next
self.remove_node(y)
x.parent
>>> AttributeError: 'NoneType' object has no attribute 'parent'
I could exchange all corresponding attributes of x and y.
But that takes pretty many lines since the structure of a node is rather complicated.
I must be missing something, because what’s wrong with this?
Or have you meant for reassigning all the names, like some kind of identity theft of an object? While it would be possible working at the C level, I would be pretty surprised if you could do it from within python without breaking things majorly.