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?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nawijn@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'attribute': 0.05; 'attributes': 0.07; 'attribute.': 0.09; 'happen.': 0.09; 'instances.': 0.09; 'instantiated': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'assume': 0.11; 'a()': 0.16; 'a(object):': 0.16; 'attributes.': 0.16; 'declaration': 0.16; 'instances,': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'example.': 0.17; 'instance': 0.17; 'visible': 0.22; 'example': 0.23; 'programming': 0.23; 'this:': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'cc:addr:gmail.com': 0.27; 'cc:2**2': 0.27; 'all.': 0.28; 'surprised': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; 'thursday,': 0.30; 'not.': 0.32; 'getting': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'test': 0.36; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'shows': 0.38; 'where': 0.40; 'think': 0.40; 'first': 0.61; '30,': 0.62; 'show': 0.63; '10.': 0.64; 'equals': 0.65; 'following.': 0.65; 'learned': 0.65; 'august': 0.66; 'today.': 0.69; '12.': 0.71; '10:11': 0.84; 'received:209.85.213.184': 0.91; 'received:mail- yx0-f184.google.com': 0.91; 'angel': 0.93
Newsgroups comp.lang.python
Date Thu, 30 Aug 2012 07:48:24 -0700 (PDT)
In-Reply-To <mailman.3972.1346337059.4697.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=137.17.109.98; posting-account=QNsEXgoAAABu_Avbf026MEK8WO7y27Mt
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>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 137.17.109.98
MIME-Version 1.0
Subject Re: class object's attribute is also the instance's attribute?
From Marco Nawijn <nawijn@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc Marco Nawijn <nawijn@gmail.com>, python-list@python.org, d@davea.name
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.3973.1346338107.4697.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346338107 news.xs4all.nl 6893 [2001:888:2000:d::a6]:56143
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28125

Show key headers only | 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