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


Groups > comp.lang.python > #20801

Re: namespace question

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 <prvs=394cb8829=jeanmichel@sequans.com>
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 <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To xixiliguo <wangbo.red@gmail.com>
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 <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>
Newsgroups comp.lang.python
Message-ID <mailman.124.1330084570.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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


Thread

namespace question xixiliguo <wangbo.red@gmail.com> - 2012-02-23 21:55 -0800
  Re: namespace question Chris Rebert <clp2@rebertia.com> - 2012-02-23 22:35 -0800
  Re: namespace question Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-02-24 12:56 +0100
  Re: namespace question David <dwblas@gmail.com> - 2012-02-24 10:08 -0800
    Re: namespace question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-24 22:25 +0000
      Re: namespace question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-25 00:39 +0000
        Re: namespace question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-25 01:38 +0000
      Re: namespace question Ben Finney <ben+python@benfinney.id.au> - 2012-02-26 19:47 +1100
        Re: namespace question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-26 13:40 +0000
          Re: namespace question Ben Finney <ben+python@benfinney.id.au> - 2012-02-28 22:36 +1100
            Re: namespace question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-28 14:06 +0000

csiph-web