Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.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; 'python,': 0.02; 'subject:Python': 0.06; 'attribute': 0.07; 'nested': 0.07; '__init__': 0.09; 'namespace': 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; 'references.': 0.09; "subject:')": 0.09; 'subject:2.7': 0.09; 'subject:method': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'assume': 0.14; 'accesses': 0.16; 'defined,': 0.16; 'defined.': 0.16; 'effect.': 0.16; 'exists,': 0.16; 'globals': 0.16; 'mechanism.': 0.16; 'nameerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'scope,': 0.16; 'self.y': 0.16; 'ties': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'header:User- Agent:1': 0.23; '---': 0.24; 'right.': 0.26; 'pass': 0.26; 'subject:/': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'code': 0.31; 'object.': 0.31; 'testing.': 0.31; 'class': 0.32; 'regular': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'subject: (': 0.35; 'there': 0.35; 'consistent': 0.36; 'functions.': 0.36; 'module.': 0.36; 'possible': 0.36; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'received:173': 0.61; 'name': 0.63; 'more': 0.64; 'obvious': 0.74; '2.7.': 0.84; 'coupled': 0.84; 'received:fios.verizon.net': 0.84; 'tie': 0.84; 'period.': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Closure/method definition question (delete 'for Python 2.7') Date: Mon, 10 Mar 2014 14:59:10 -0400 References: <71E0ECF7BE49E84CBD47914C9E19FFED2793AD54@exchm-omf-22.exelonds.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: <71E0ECF7BE49E84CBD47914C9E19FFED2793AD54@exchm-omf-22.exelonds.com> 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394477981 news.xs4all.nl 2949 [2001:888:2000:d::a6]:54087 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68169 On 3/10/2014 1:27 PM, Brunick, Gerard:(Constellation) wrote: > class Test(object): > x = 10 > > def __init__(self): > self.y = x > > t = Test() > --- > > raises > > NameError: global name 'x' is not defined. > > in Python 2.7. In Python, period. > I would assume that when __init__ is being defined, > it is just a regular old function Right. It is an attribute of the class much like other object. This is more obvious and more consistent in Python 3. In Python 3, these two code snippets have the same effect. class C: def f(self): return self.a g = C.f # in2.x, C.f.im_func def g(self): return self.a class C: pass C.f = g This is possible because functions are only loosely coupled to the class they are defined in, through the public attribute mechanism. This is handy for testing. There is no private namespace tie. As long as self.a exists, g could function even if the class C object were deleted or inaccessible. A function that accesses module globals *does* have a private namespace tie to the module it is defined in (lexical scoping), and this must be accounted for when testing or otherwise using it in another module -- perhaps by altering the original module. > and x is a variable in an outer scope, Non-global 'outer scope' is peculiar to lexically nested functions. Any nonlocal access ties a function to its outer function through private internal references. -- Terry Jan Reedy