On 2014-03-10, Marko Rauhamaa <ma...@pacujo.net> wrote: > "Brunick, Gerard:(Constellation)" <gerard.brun...@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 -- https://mail.python.org/mailman/listinfo/python-list