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


Groups > comp.lang.python > #20781

Re: namespace question

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 <chris@rebertia.com>
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 <clp2@rebertia.com>
To xixiliguo <wangbo.red@gmail.com>
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 <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.109.1330065309.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, Feb 23, 2012 at 9:55 PM, xixiliguo <wangbo.red@gmail.com> wrote:
> c = [1, 2, 3, 4, 5]
> class TEST():
>    c = [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.

>    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

Python is not Java (or similar). To refer to instance variables, you
must explicitly use `self`; i.e. use "self.c[0] = 15" in add().

I would recommend reviewing the relevant section of the Python tutorial:
http://docs.python.org/tutorial/classes.html

Cheers,
Chris

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