Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'assign': 0.07; 'latter': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:2.7': 0.09; 'subject:method': 0.09; 'subject:question': 0.10; 'def': 0.12; 'defined.': 0.16; 'hides': 0.16; 'nameerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scope.': 0.16; 'self.y': 0.16; 'variable.': 0.16; 'ways:': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'header:User-Agent:1': 0.23; '---': 0.24; 'subject:/': 0.26; 'header:X-Complaints-To:1': 0.27; 'class': 0.32; 'skip:_ 10': 0.34; 'module.': 0.36; 'charset:us-ascii': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'new': 0.61; "you'll": 0.62; 'name': 0.63; 'refer': 0.63; 'or:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neil Cerutti Subject: Re: Closure/method definition question for Python 2.7 Date: Tue, 11 Mar 2014 13:20:35 +0000 (UTC) Organization: Norwich University References: <87vbvmq7l3.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: slrn/0.9.9p1/mm/ao (Win32) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394544066 news.xs4all.nl 2917 [2001:888:2000:d::a6]:59053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68216 On 2014-03-10, Marko Rauhamaa wrote: > "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 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