Swap doesn't work for the object attributes in Python?
<p>I write the heap sort code is as follows:</p>

<p>What is interesting is </p>

<pre><code>self.first, self.last = self.last, self.first 
</code></pre>

<p>is not swapping the two values in the head and tail of the list <code>self.list</code>.</p>

<p>Why?</p>

<pre><code>#heapsort
import pdb
class heap(object):
    def __init__(self, list):
        super(heap, self).__init__()
        self.list = list
        self.heapify()
        if self.list:
            self.first = self.list[0]
            self.last = self.list[len(self.list)-1]
    def length(self):
        return len(self.list)
    def heap_node(self,i):
        large_i = i
        if 2*i+1&lt;self.length() and self.list[2*i+1]&gt;self.list[i]:
            large_i = 2*i+1
        if 2*i+2&lt;self.length() and self.list[2*i+2]&gt;self.list[large_i]:
            large_i = 2*i+2
        if large_i!=i:
            self.list[large_i], self.list[i] = self.list[i], self.list[large_i]
            self.heap_node(large_i)
    def heapify(self):
            for i in range(self.length()/2,0,-1):
                j=i-1
                self.heap_node(j)
    def sort(self):
        # pdb.set_trace()
        if not self.list:
            return []
        else:
            # self.list[0], self.list[len(self.list)-1] = self.list[len(self.list)-1], self.list[0]
            self.first, self.last = self.last, self.first 
            return heap(self.list[0:(self.length()-1)]).sort() + [self.list[len(self.list)-1]]

h=heap([1,3,2,5,4])
print h.list
print h.sort()
</code></pre>
