Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28125

Re: class object's attribute is also the instance's attribute?

Newsgroups comp.lang.python
Date 2012-08-30 07:48 -0700
References <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com> <e8b14404-2340-4743-b66b-e9afb701e676@googlegroups.com> <503f69c2$0$6872$e4fe514c@news2.news.xs4all.nl> <9246f6a0-b570-461d-b3a3-818b7138531a@googlegroups.com> <mailman.3972.1346337059.4697.python-list@python.org>
Subject Re: class object's attribute is also the instance's attribute?
From Marco Nawijn <nawijn@gmail.com>
Message-ID <mailman.3973.1346338107.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Thursday, August 30, 2012 4:30:59 PM UTC+2, Dave Angel wrote:
> On 08/30/2012 10:11 AM, Marco Nawijn wrote:
> 
> > On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
> 
> >> <snip>
> 
> >>
> 
> > Learned my lesson today. Don't assume you know something. Test it first ;). I have done quite some programming in Python, but did not know that class attributes are still local to the instances. 
> 
> 
> 
> They're not.  They're just visible to the instances, except where the
> 
> instance has an instance attribute of the same name.  Don't be confused
> 
> by dir(), which shows both instance and class attributes.
> 
> 
> 
> Please show me an example where you think you observe each instance
> 
> getting a copy of the class attribute.  There's probably some other
> 
> explanation.

I don't have an example. It was just what I thought would happen. Consider the following. In a class declaration like this:

class A(object):
    attr_1 = 10

    def __init__(self):
       self.attr_2 = 20

If I instantiated it twice:

obj_1 = A()
obj_2 = A()

For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after the following statement:

obj_1.attr_1 = 12

is that obj_2.attr_1 also equals 12. This is what surprised me a little, that's all. 

Marco

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 03:55 -0700
  Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 07:53 -0400
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
  Re: class object's attribute is also the instance's attribute? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 13:22 +0200
  Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 05:34 -0700
    Re: class object's attribute is also the instance's attribute? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-08-30 13:52 +0100
    Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 15:25 +0200
      Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:11 -0700
        Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 10:30 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 11:18 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 17:20 +0200
              Re: class object's attribute is also the instance's attribute? Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 09:58 +1000

csiph-web