Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'attribute': 0.05; 'attributes': 0.07; 'assigning': 0.09; 'attribute.': 0.09; 'happen.': 0.09; 'instance.': 0.09; 'instances.': 0.09; 'instantiated': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'assume': 0.11; 'a()': 0.16; 'a(object):': 0.16; 'attribute,': 0.16; 'attributes.': 0.16; 'declaration': 0.16; 'instances,': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'subject:object': 0.16; 'surprising': 0.16; 'wrote:': 0.17; 'example.': 0.17; 'instance': 0.17; '>>>': 0.18; 'modifying': 0.22; 'visible': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'programming': 0.23; 'statement': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'all.': 0.28; '>>>>': 0.29; 'surprised': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; 'thursday,': 0.30; 'not.': 0.32; 'print': 0.32; 'getting': 0.33; 'done': 0.34; 'adds': 0.35; 'subject:?': 0.35; 'except': 0.36; 'but': 0.36; 'test': 0.36; 'does': 0.37; 'quite': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'shows': 0.38; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'first': 0.61; "you'll": 0.62; '30,': 0.62; 'show': 0.63; '10.': 0.64; 'equals': 0.65; 'following.': 0.65; 'learned': 0.65; 'august': 0.66; 'header:Reply-To:1': 0.68; 'believe': 0.69; 'today.': 0.69; '12.': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; '10:11': 0.84; 'received:74.208.4.194': 0.84; 'angel': 0.93 Date: Thu, 30 Aug 2012 11:18:26 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Marco Nawijn Subject: Re: class object's attribute is also the instance's attribute? References: <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com> <503f69c2$0$6872$e4fe514c@news2.news.xs4all.nl> <9246f6a0-b570-461d-b3a3-818b7138531a@googlegroups.com> <721544fd-8734-49cd-b43e-210d61d03f9b@googlegroups.com> In-Reply-To: <721544fd-8734-49cd-b43e-210d61d03f9b@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:FcJollWZCiLEddk9ESNdOAttrcaRkW5sgaQOQmtoFEQ wr968ESYUh/QEZIALG0GKC9mPTK8eTmc0UBbaq9/rkENDxnE6u 3lgqvm6N96KgM35kXF3BEpb0zck/qmTVWVIUexDcVFaIqLCrwB fHxl/f0kW00shIqEy9VoXqPdJZmax/F9EG9sNreaYFnyu7raZs wU5WdqPOXTuebMAw3vUkbxZ5c9QWpd6D5SYuhVZikLviCNKn7i NLX1gYFIRiLKQ5MIuANxm0dGlvQZxOnpTSKJ21BWtr3en7ISi5 1cCPm0mZYcgp6XdxXgDdXXhvKjjxh4RMr6rmB8pY5sR+IA8vg= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346339935 news.xs4all.nl 6949 [2001:888:2000:d::a6]:42150 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28128 On 08/30/2012 10:48 AM, Marco Nawijn wrote: > 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: >> >>>> >> >>>> >> >>> 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 > That statement only adds an instance attribute, not modifying the class attribute of the same name. But it does "hide" it from that particular instance. The thing that can be surprising is that if the class attribute is mutable, and you mutate it, rather than assigning it. So for example: class A(object): attr_1 = [10, 9] def __init__(self): self.attr_2 = 20 obj_1 = A() obj_2 = A() obj_1.attr_1.append(3) Then I believe you'll see [10, 9, 3] from both instances. print obj_1.attr_1 print obj_2.attr_1 -- DaveA