Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.05; 'attributes': 0.05; 'instance,': 0.05; 'attribute': 0.07; 'statements,': 0.07; 'python': 0.08; 'globals': 0.09; 'def': 0.13; 'block,': 0.16; 'illustration': 0.16; 'namespace.': 0.16; 'naming': 0.16; 'strange,': 0.16; 'test()': 0.16; 'test():': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'cc:2**0': 0.26; '(in': 0.26; 'class': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'description.': 0.30; 'usually': 0.31; 'quite': 0.31; 'objects': 0.32; 'languages': 0.32; 'header:User- Agent:1': 0.33; 'named': 0.33; 'file': 0.34; 'but': 0.37; 'not,': 0.38; 'skip:_ 10': 0.38; 'some': 0.38; 'unlike': 0.39; 'put': 0.40; 'here': 0.64; 'owner,': 0.68; "'class'": 0.84; "'local'": 0.84; 'locals': 0.84; 'valid,': 0.84 X-IronPort-AV: E=Sophos;i="4.73,475,1325458800"; d="scan'208";a="188828" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Fri, 24 Feb 2012 12:56:01 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: xixiliguo Subject: Re: namespace question References: <4895480.4960.1330062902417.JavaMail.geo-discussion-forums@ynjd19> In-Reply-To: <4895480.4960.1330062902417.JavaMail.geo-discussion-forums@ynjd19> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330084570 news.xs4all.nl 6899 [2001:888:2000:d::a6]:53419 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20801 xixiliguo wrote: > c = [1, 2, 3, 4, 5] > class TEST(): > c = [5, 2, 3, 4, 5] > def add( self ): > c[0] = 15 > > a = TEST() > > > a.add() > > print( c, a.c, TEST.c ) > > result : > [15, 2, 3, 4, 5] [5, 2, 3, 4, 5] [5, 2, 3, 4, 5] > > > why a.add() do not update c in Class TEST? but update c in main file > Attributes can only accessed by explictly naming the owner, unlike some other languages which infer 'this/self'. When an attribute is not found in the owner, python may look into the "outer" namespace. Read the python documentation for an accurate description. Here is an illustration (python 2.5): c='global' class TEST(): c = 'class' d = 'classonly' def __init__(self): self.c='instance' def test(self): print c print TEST.c print self.c print self.d # this is valid, if d is not found in the instance, python will look into the class t = TEST() t.test() global class instance classonly Note that objects in python are properly named namespaces. locals and globals are not, so be careful while naming those (in few words: don't use globals) c = 'global' def foo(): c = 'local' print c # same name for 2 different objects def bar(): print c global c # the global statement is quite strange, it applies to the whole block, even previous statements, ppl usually put it at the begining of the block though foo() bar() 'local' 'global' Cheers, JM