Python - How do I differentiate between two list elements that point to the same object?
<p>I have a Ring structure implemented as follows (based on a cookbook recipe I found):</p>

<pre><code>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()
</code></pre>

<p>Say I do the following:</p>

<pre><code>x = Ring([1,2,3,4,4])
x.setTop(4)
</code></pre>

<p>My code will always set the first 4 (currently x[3]) to x[0]. It seems (via object identity and hash id testing between x[3] and x[4]) that Python is reusing the 4 object.</p>

<p>How do I tell Python that I really want the second 4 (currently x[4]) to be at the top?</p>

<p>Apologies for the basic question ... one of the downfalls of being a self-taught beginner.</p>

<p>Thanks,</p>

<p>Mike</p>

<p>===EDIT===</p>

<p>For what it's worth, I dropped the setTop method from the class. I had added it to the standard recipe thinking "hey, this would be neat and might be useful." As the answers (esp. "what's the difference", which is spot on) and my own experience using the structure show, it's a crappy method that doesn't support any of my use cases.</p>

<p>In other words, adding something because I could instead of fulfilling a need = fail.</p>
