Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'variable,': 0.07; 'python': 0.08; 'variables,': 0.09; 'method.': 0.15; 'test()': 0.16; 'test():': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.16; '(i.e.': 0.17; 'wrote:': 0.18; 'instance': 0.18; 'cheers,': 0.20; 'java': 0.21; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; '(or': 0.22; 'feb': 0.22; 'unlikely': 0.23; 'cc:2**0': 0.26; 'message- id:@mail.gmail.com': 0.29; 'explicitly': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'chris': 0.30; 'creates': 0.31; 'thu,': 0.32; 'file': 0.34; 'normally': 0.34; 'url:python': 0.35; 'received:209.85.214': 0.36; 'variables': 0.37; 'created': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'i.e.': 0.39; 'url:org': 0.39; 'received:209': 0.39; 'header:Received:6': 0.61; 'body': 0.61; 'relevant': 0.71; 'sender:addr:chris': 0.84 Received-SPF: pass (google.com: domain of chris@rebertia.com designates 10.204.129.18 as permitted sender) client-ip=10.204.129.18; Authentication-Results: mr.google.com; spf=pass (google.com: domain of chris@rebertia.com designates 10.204.129.18 as permitted sender) smtp.mail=chris@rebertia.com; dkim=pass header.i=chris@rebertia.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=Luq8PAjr85wgVSkV4YhExKTjddl08srIXBQJwS71Cvs=; b=PPwZEYSX6SjjhLozFhzH2JyATv6iHLCacbrMnugcy1ELgEnAUlpEHU81bsKG/B9UEP C8vMFvC1UrZecV5P0WIrCqX5bmOXCK+OT4feyijhSn9eyO0WTKUNeTTcc3mHH3DyRJwY KIthJLgKQ23TOxGw2/OE1vLI1I4J5r49u3aGo= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4895480.4960.1330062902417.JavaMail.geo-discussion-forums@ynjd19> References: <4895480.4960.1330062902417.JavaMail.geo-discussion-forums@ynjd19> Date: Thu, 23 Feb 2012 22:35:07 -0800 X-Google-Sender-Auth: 3D2d5HFpcnzzzobznKJ1vnGqPsA Subject: Re: namespace question From: Chris Rebert To: xixiliguo Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQnKz0b8XKzTN7Tq6boAIqbrnbChPnQ8EJH/91XIOtsJCF/dTtZgf8A1W6dKAFk9V4ClnzQ9 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330065309 news.xs4all.nl 6912 [2001:888:2000:d::a6]:38351 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20781 On Thu, Feb 23, 2012 at 9:55 PM, xixiliguo wrote: > c =3D [1, 2, 3, 4, 5] > class TEST(): > =C2=A0 =C2=A0c =3D [5, 2, 3, 4, 5] That line creates a class (i.e. "static") variable, which is unlikely to be what you want. Instance variables are normally created in the body of an __init__() method. > =C2=A0 =C2=A0def add( self ): > =C2=A0 =C2=A0 =C2=A0 =C2=A0c[0] =3D 15 > > a =3D 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 Python is not Java (or similar). To refer to instance variables, you must explicitly use `self`; i.e. use "self.c[0] =3D 15" in add(). I would recommend reviewing the relevant section of the Python tutorial: http://docs.python.org/tutorial/classes.html Cheers, Chris