Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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; 'variables': 0.07; '__init__': 0.09; 'attributes': 0.09; 'considered.': 0.09; 'created,': 0.09; 'equivalent;': 0.09; 'moreover,': 0.09; 'raises': 0.09; 'subject:2.7': 0.09; 'subject:method': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'defined,': 0.16; 'defined.': 0.16; 'nameerror:': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'self.y': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'why.': 0.24; 'fine': 0.24; 'mon,': 0.24; '---': 0.24; 'subject:/': 0.26; 'code:': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'fine,': 0.31; 'modified,': 0.31; 'class': 0.32; 'regular': 0.32; 'skip:_ 10': 0.34; 'created': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'example,': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'then,': 0.60; 'name': 0.63; 'skip:n 10': 0.64; 'close': 0.67; 'mar': 0.68; '2.7.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=r9GAGpbOoLS36A1oZok0DrSwC9EHNyX4E47OPiW+lZ0=; b=lHJt7wfFw5YenGxBF7S3Jscyylmht8tnXF/siXyT5qgK3Ibzcjjyk5J+EM4tR/T29J zevwrJW48zXRWl8AWQiR9ZYCOOdxR/7L6RrEEiT14fCd5lKhTBAhWM7STvDuuWXH0aF8 qGdltZDEmg9c6oLNdEhGgPOO1JXpIgxPQ9rWN4Xs8KeJjkuCTD0xYwjGiv0+bSVU9PHx q1mXzlo/J/cNo0axFPu45SZcnY+PTIHNGocxkcCgvwitBmnQo0g3OjSz85vxFowSdqRz R05ZsWO6GgLhyMa7fTL3xYlxPeQVhtwKDrXuUCqSX6UupoJQehxbnxeZwK9mBmOSQdnu 61Dg== X-Received: by 10.67.13.134 with SMTP id ey6mr43387318pad.44.1394497627396; Mon, 10 Mar 2014 17:27:07 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <71E0ECF7BE49E84CBD47914C9E19FFED2793AD54@exchm-omf-22.exelonds.com> References: <71E0ECF7BE49E84CBD47914C9E19FFED2793AD54@exchm-omf-22.exelonds.com> From: Ian Kelly Date: Mon, 10 Mar 2014 18:26:27 -0600 Subject: Re: Closure/method definition question for Python 2.7 To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394497630 news.xs4all.nl 2905 [2001:888:2000:d::a6]:50906 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68184 On Mon, Mar 10, 2014 at 11:27 AM, Brunick, Gerard:(Constellation) wrote: > The following code: > > --- > class Test(object): > x =3D 10 > > def __init__(self): > self.y =3D x > > t =3D Test() > --- > > raises > > NameError: global name 'x' is not defined. > > in Python 2.7. I don't understand why. I would assume that when __init_= _ is being defined, it is just a regular old function and x is a variable i= n an outer scope, so the function __init__ would close over the variable x.= Moreover, the variable x is not being modified, so this should be O.K. F= or example, the following is fine (if nonsensical): Class scopes and function scopes are not equivalent; class attributes are not considered for closures. Only local variables of outer functions are considered. At the time __init__ is defined the Test class has not been created yet, and so the variable x is in local scope then, e.g.: class Test(object): x =3D 10 def __init__(self, y=3Dx): self.y =3D y This works fine, because the x is evaluated when the function is created, while it's still in local scope. But Python won't create a closure for it.