I have a Ring structure implemented as follows (based on the cookbook recipe I found):
class Ring(list): def turn(self): last = self.pop(0) self.append(last) def setTop(self, objectReference): if objectReference not in self: raise ValueError, "object is not in ring" while self[0] is not objectReference: self.turn()
Let's say I do the following:
x = Ring([1,2,3,4,4]) x.setTop(4)
My code will always set the first 4 (currently x [3]) to x [0]. It seems (through an object identifier and a hash identifier between x [3] and x [4]) that Python reuses 4 objects.
How do I tell Python that I really want the second 4 (currently x [4]) to be on top?
Sorry for the basic question ... one of the obstacles to becoming a self-taught newbie.
Thanks,
Mike
=== EDIT ===
For what it's worth, I dropped the setTop method from the class. I added it to the standard recipes, thinking "hey, that would be neat and could be helpful." Like the answers (like “what's the difference” that is in place) and my own experience with using the structure show this is a shitty method that does not support any of my use cases.
In other words, adding something because I can instead fulfill the need = crash.
source share