Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68216
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Subject | Re: Closure/method definition question for Python 2.7 |
| Date | 2014-03-11 13:20 +0000 |
| Organization | Norwich University |
| References | <mailman.8014.1394472539.18130.python-list@python.org> <87vbvmq7l3.fsf@elektro.pacujo.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8046.1394544066.18130.python-list@python.org> (permalink) |
On 2014-03-10, Marko Rauhamaa <marko@pacujo.net> wrote: > "Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>: > >> class Test(object): >> x = 10 >> >> def __init__(self): >> self.y = x >> >> t = Test() >> --- >> >> raises >> >> NameError: global name 'x' is not defined. > > In the snippet, x is neither local to __init__() nor global to > the module. It is in the class scope. You can refer to it in > one of two ways: > > Test.x > > or: > > self.x The latter will work only to read the class variable. If you assign to self.x you'll create a new instance variable that hides the class variable. -- Neil Cerutti
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Closure/method definition question for Python 2.7 "Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com> - 2014-03-10 17:27 +0000
Re: Closure/method definition question for Python 2.7 Marko Rauhamaa <marko@pacujo.net> - 2014-03-10 19:36 +0200
Re: Closure/method definition question for Python 2.7 Neil Cerutti <neilc@norwich.edu> - 2014-03-11 13:20 +0000
csiph-web