class members vs instance members

Discussion in 'Python' started by hdixon, Jul 9, 2006.

  1. hdixon

    hdixon Guest

    Ive spent a few days going thru a couple of Python tutorials. No
    problem until I got to classes. I guess my java mindset is blocking my
    vision. I've borrowed another thread's code snippet and cannot explain
    the results:
    class MyClass:
    list = []
    myvar = 10

    def add(self, x):
    self.list.append(x)
    self.myvar = x

    def printer(self):
    print self.list
    print self.myvar

    a = MyClass()
    b = MyClass()

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

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

    This produces:
    list in a:
    list in a:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    19
    list in b:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    10

    WHY do the "class members" list and myvar seem to behave differently?
    Given the way that list[] is supposedly shared why doesnt myvar exhibit
    the same behavior? It seems that myvar is acting like a true "instance
    member". Is this simply because of the underlying types?


    TIA
     
    hdixon, Jul 9, 2006
    #1
    1. Advertising

  2. hdixon enlightened us with:
    > class MyClass:
    > list = []
    > myvar = 10
    >
    > def add(self, x):
    > self.list.append(x)
    > self.myvar = x


    Here is your difference. In the add() function, self.myvar is assigned
    a value. This means self.myvar is an instance variable. If you had
    assigned MyClass.myvar = x, then it would have been as you expected.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jul 9, 2006
    #2
    1. Advertising

  3. hdixon a écrit :
    > Ive spent a few days going thru a couple of Python tutorials. No
    > problem until I got to classes. I guess my java mindset is blocking my
    > vision.


    Then http://dirtsimple.org/2004/12/python-is-not-java.html

    > I've borrowed another thread's code snippet and cannot explain
    > the results:
    > class MyClass:


    class MyClass(object):

    > list = []


    This may not be too harmful in the given context, nut shadowing builtin
    types may not be a good idea.

    > myvar = 10
    >
    > def add(self, x):
    > self.list.append(x)


    "list" is a class attribute. It's shared by all instances of MyClass.
    This is a FAQ AFAICT.

    > self.myvar = x


    This creates an instance attribute named myvar that shadows the class
    attribute of the same name

    (snip expected results)

    > WHY do the "class members" list and myvar seem to behave differently?


    cf above. And notice that in Python, binding (aka 'assignement') and
    name lookup on objects are different beasts. There have been quite a few
    threads about this recently.

    > Given the way that list[] is supposedly shared why doesnt myvar exhibit
    > the same behavior?


    Because binding to self.myvar creates the instance attribute "myvar".
    Notice that you have not rebound "list".

    > It seems that myvar is acting like a true "instance
    > member".


    It is one.

    > Is this simply because of the underlying types?


    Nope, it's because in the first case (MyClass.list, accessed as
    self.list) you just call methods on the list object, which has no effect
    on the binding.
     
    Bruno Desthuilliers, Jul 9, 2006
    #3
  4. hdixon

    hdixon Guest

    Bruno Desthuilliers wrote:
    > hdixon a écrit :
    > > Ive spent a few days going thru a couple of Python tutorials. No
    > > problem until I got to classes. I guess my java mindset is blocking my
    > > vision.

    >
    > Then http://dirtsimple.org/2004/12/python-is-not-java.html
    >
    > > I've borrowed another thread's code snippet and cannot explain
    > > the results:
    > > class MyClass:

    >
    > class MyClass(object):
    >
    > > list = []

    >
    > This may not be too harmful in the given context, nut shadowing builtin
    > types may not be a good idea.
    >
    > > myvar = 10
    > >
    > > def add(self, x):
    > > self.list.append(x)

    >
    > "list" is a class attribute. It's shared by all instances of MyClass.
    > This is a FAQ AFAICT.
    >
    > > self.myvar = x

    >
    > This creates an instance attribute named myvar that shadows the class
    > attribute of the same name
    >
    > (snip expected results)
    >
    > > WHY do the "class members" list and myvar seem to behave differently?

    >
    > cf above. And notice that in Python, binding (aka 'assignement') and
    > name lookup on objects are different beasts. There have been quite a few
    > threads about this recently.
    >
    > > Given the way that list[] is supposedly shared why doesnt myvar exhibit
    > > the same behavior?

    >
    > Because binding to self.myvar creates the instance attribute "myvar".
    > Notice that you have not rebound "list".
    >
    > > It seems that myvar is acting like a true "instance
    > > member".

    >
    > It is one.
    >
    > > Is this simply because of the underlying types?

    >
    > Nope, it's because in the first case (MyClass.list, accessed as
    > self.list) you just call methods on the list object, which has no effect
    > on the binding.


    ok - i "think" its beginning to dawn on me. BTW - the blog above was a
    good read for me. I _am_ trying to bring java into the mix and its
    screwing me up big time. I'll go back over the chapters that dicuss
    binding/name lookups.


    Thanks
     
    hdixon, Jul 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Suzanne Vogel
    Replies:
    5
    Views:
    2,362
    Dan W.
    Dec 9, 2003
  2. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  3. Sridhar R
    Replies:
    14
    Views:
    1,409
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  4. Gerry Sutton
    Replies:
    1
    Views:
    543
    Peter Otten
    Apr 16, 2005
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    377
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page