Python instances

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • henrikpierrou@hotmail.com

    Python instances

    Hi,

    How do python instances work?
    Why does the code at the end of my posting produce this output:

    list in a:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    list in b:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    instead of

    list in a:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    list in b:
    []

    ----------------------------

    class MyClass:
    list = []

    def add(self, x):
    self.list.appen d(x)

    def printer(self):
    print self.list

    a = MyClass()
    b = MyClass()

    for n in range(10):
    a.add(n)

    print "list in a:"
    a.printer()
    print "list in b:"
    b.printer()

    /H

  • Roland Heiber

    #2
    Re: Python instances

    Hi,
    [color=blue]
    > class MyClass:
    > list = [][/color]

    you have "list" defined as a classmember, not an instancemember. So
    "list" ist defined ONCE for all instances.

    Try this instead:

    class MyClass:
    def __init__(self):
    self.list = []
    [...]

    and use self.list ...

    HtH, Roland

    Comment

    • henrikpierrou@hotmail.com

      #3
      Re: Python instances

      Guess i shouldn't think of the __init__(self) function as a constructor
      then.
      Thanks.

      /H

      Comment

      • Laszlo Zsolt Nagy

        #4
        Re: Python instances

        [color=blue]
        >Guess i shouldn't think of the __init__(self) function as a constructor
        >then.
        >
        >[/color]
        __init__ is THE constructor in Python


        --
        _______________ _______________ _______________ _______________ _____
        Laszlo Nagy web: http://designasign.biz
        IT Consultant mail: gandalf@geochem source.com

        Python forever!


        Comment

        • Bengt Richter

          #5
          Re: Python instances

          On 20 Apr 2005 00:44:53 -0700, henrikpierrou@h otmail.com wrote:
          [color=blue]
          >Guess i shouldn't think of the __init__(self) function as a constructor
          >then.
          >Thanks.[/color]
          Depends on what you think when you think "constructo r" ;-)
          Read about both __new__ and __init__. The former is always
          necessary to create an object, and __init__ may take parameters to
          define intial state from its parameters, but __new__ does the whole
          job for immutables. I.e., "constructo r" translates to combination of
          both if both are present, but __new__ must be always be there and
          come first. In general there are default methods inherited from
          object and/or type, the most primitive classes, so you don't have
          to define them except to customize for your purposes.
          At least, that's the way I think of it ;-)

          Regards,
          Bengt Richter

          Comment

          • Kent Johnson

            #6
            Re: Python instances

            henrikpierrou@h otmail.com wrote:[color=blue]
            > Guess i shouldn't think of the __init__(self) function as a constructor
            > then.[/color]

            No, that's not it. You shouldn't think of variables defined outside of a method as instance variables.

            In Java for example you can write something like

            public class MyClass {
            private List list = new ArrayList();

            public void add(Object x) {
            list.add(x);
            }
            }

            In this case list is a member variable of MyClass instances; 'this' is implicit in Java.

            In Python, if you write something that looks similar, the meaning is different:

            class MyClass:
            list = []

            def add(self, x):
            self.list.appen d(x)

            In this case, list is an attribute of the class. The Java equivalent is a static attribute. In
            Python, instance attributes have to be explicitly specified using 'self'. So instance attributes
            have to be bound in an instance method (where 'self' is available):

            class MyClass:
            def __init__(self):
            self.list = []

            def add(self, x):
            self.list.appen d(x)

            Kent

            Comment

            • EuGeNe

              #7
              Re: Python instances

              henrikpierrou@h otmail.com wrote:[color=blue]
              > Hi,
              >
              > How do python instances work?
              > Why does the code at the end of my posting produce this output:
              >
              > list in a:
              > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
              > list in b:
              > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
              >
              > instead of
              >
              > list in a:
              > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
              > list in b:
              > []
              >
              > ----------------------------
              >
              > class MyClass:
              > list = []
              >
              > def add(self, x):
              > self.list.appen d(x)
              >
              > def printer(self):
              > print self.list
              >
              > a = MyClass()
              > b = MyClass()
              >
              > for n in range(10):
              > a.add(n)
              >
              > print "list in a:"
              > a.printer()
              > print "list in b:"
              > b.printer()
              >
              > /H
              >[/color]

              because list is a class member not an instance member (not sure about
              the vocabulary) if in __init__ you set self.list=[] you'll get the
              result you want! By declaring list=[] in the class it is shared between
              all instances!

              --
              EuGeNe

              [----



              ----]

              Comment

              Working...