Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20781
| References | <4895480.4960.1330062902417.JavaMail.geo-discussion-forums@ynjd19> |
|---|---|
| Date | 2012-02-23 22:35 -0800 |
| Subject | Re: namespace question |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.109.1330065309.3037.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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