Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: Closure/method definition question for Python 2.7 Date: Mon, 10 Mar 2014 19:36:24 +0200 Organization: A noiseless patient Spider Lines: 27 Message-ID: <87vbvmq7l3.fsf@elektro.pacujo.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx05.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="2851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kaY/Zk6walpX9XRJshgSE" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) Cancel-Lock: sha1:iNgplh31L0NaZEdP5VWCwYC6A4M= sha1:ZMyy15rz/mPMCBl1au2/x+Tmoh8= Xref: csiph.com comp.lang.python:68167 "Brunick, Gerard:(Constellation)" : > 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 Marko