Problem with class variables

Dag f98dawa at dd.chalmers.se
Thu Mar 29 09:29:21 EDT 2007


I have a problem which is probaly very simple to solve, but I can't
find a nice solution.
I have the following code 

class Test:
    def __init__(self):
        self.a=[1,2,3,4]
        self.b=self.a
    def swap(self):
        self.a[0],self.a[3]=self.a[3],self.a[0]
    def prnt(self):
        print self.a
        print self.b
c=Test()
c.swap()
c.prnt()

which returns 
[4,2,3,1]
[4,2,3,1]

Now I understand why it is doing this, but it's not what I want.  How to
I get it to return
[4,2,3,1]
[1,2,3,4]

Dag 



More information about the Python-list mailing list