Problem with class variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dag

    #1

    Problem with class variables

    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
  • Marc 'BlackJack' Rintsch

    #2
    Re: Problem with class variables

    In <slrnf0nfph.8f9 .f98dawa@remote 1.student.chalm ers.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

    Comment

    • kyosohma@gmail.com

      #3
      Re: Problem with class variables

      On Mar 29, 8:43 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
      In <slrnf0nfph.8f9 .f98d...@remote 1.student.chalm ers.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

      Comment

      • =?ISO-8859-15?Q?Thomas_Kr=FCger?=

        #4
        Re: Problem with class variables

        Dag schrieb:
        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
        Use a copy of self.a:
        self.b = self.a[:]

        Thomas

        Comment

        Working...