Problem with class variables

kyosohma at gmail.com kyosohma at gmail.com
Thu Mar 29 09:53:47 EDT 2007


On Mar 29, 8:43 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> In <slrnf0nfph.8f9.f98d... at remote1.student.chalmers.se>, Dag wrote:
> > 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
>
>           self.b = list(self.a)
>
> BTW this is about instance variable, not class variables.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

One of the many ways to do this:

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]
        return self.a
    def prnt(self):
        print self.a
        x = self.swap()
        print x

c=Test()
c.prnt()


Mike




More information about the Python-list mailing list