Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: A newbie quesiton: local variable in a nested funciton Date: Sun, 27 Dec 2015 17:32:17 +1100 Lines: 43 Message-ID: References: <701dd0e6-a9c1-4aa9-a3a2-6607cd3f3759@googlegroups.com> <661a13a1-8084-41ca-b458-297462dc3367@googlegroups.com> <0bf611ba-c4d9-4465-8d61-6a94bcee79a4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de xNmNhQbmfLwDQ8sZFyz+mw3euDaykGJFa3ofQHTI/fiQ== 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; 'received:209.85.223': 0.03; 'defines': 0.07; 'executed': 0.07; 'cc:addr:python-list': 0.09; '-------': 0.09; 'methods,': 0.09; 'statements': 0.09; 'python': 0.10; 'anyway': 0.11; 'def': 0.13; 'applies': 0.15; '"def"': 0.16; 'attributes.': 0.16; 'debugged': 0.16; 'deferred': 0.16; 'definition.': 0.16; 'distinct': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'subject:variable': 0.16; 'test()': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'module,': 0.18; 'creates': 0.18; 'runs': 0.18; 'variable': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'code.': 0.23; 'seems': 0.23; 'dec': 0.23; 'references': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'not.': 0.27; 'order.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'function': 0.28; 'referenced': 0.29; 'style.': 0.29; 'class.': 0.30; '--------': 0.32; 'noticed': 0.32; 'run': 0.33; 'class': 0.33; 'builds': 0.33; 'definition': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'instance': 0.35; "isn't": 0.35; 'but': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'say': 0.37; 'itself': 0.38; 'received:209': 0.38; 'names': 0.38; 'test': 0.39; 'does': 0.39; 'body': 0.61; 'further': 0.62; 'here.': 0.62; 'inherit': 0.66; 'results': 0.66; 'night': 0.67; 'worth': 0.67; 'chrisa': 0.84; 'construct': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=W5dKElidPobtTO7sSrXIKOHOIT1Y4+A3qqglzgfNrKM=; b=DKREtbQ7wDJzLouaF+DjKJ2EEvRhcgVCA1PRuxCnBbPqYkDlCIZUAVmgcneJlFK0VC wWKCFubRBkkhAw+TcmIpkSkKCnrTGYTLXLy9uRf9dqaUGMIi2Tk3FHw77+wLh7Kbbepf vK4mcUSoM7bM0/W78AivnBPHlGJNykHzrHmEHrFe8L+EknK7sw0vWzH0rNRccfr+0XZi WCNHY7jtNjfwwuBSiYCa/Bv7tItzcjBFXRuCuZDlI4GNTTuuxCtQpZdaH09XlKw8Bffd 7pybOhkC4dUqnumeX5pupIidyxhOsLi2bSxsLEw5vcKVnRgtXDz2fD+Bp3CMqPFcIF8F x3bQ== X-Received: by 10.107.47.162 with SMTP id v34mr1367883iov.19.1451197937979; Sat, 26 Dec 2015 22:32:17 -0800 (PST) In-Reply-To: <0bf611ba-c4d9-4465-8d61-6a94bcee79a4@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100903 On Sun, Dec 27, 2015 at 3:11 PM, wrote: > Last night I noticed that Python does not resolve name in "def" during im= port, as C does in the compile/link stage, it was deferred until it was ref= erenced (i.e. codes was executed). That's OK for Anyway codes has to be deb= ugged sooner or later. I just have to get used to this style. > > But check these codes, it seems not. > ------- > x =3D 1 # a global variable > print(x) > > class Test: > x =3D 4 # a class attribute > print(x) > def func(self): > print(x) > > x1 =3D Test() > x1.x =3D 41 # a instance's attribute > x1.func() # it's 1 but 41 was expect:-( > -------- > > --Jach When you import this module, it runs all top-level code. So the 'print' at the top will happen at import time. Among the top-level statements is a class definition. When that gets run (to construct the class itself - distinct from instantiating it, which happens further down), it builds a class by executing all the statements in it, in order. That results in that value of x being printed, and then defines a function. The function definition is being run at time of class construction, and it creates a new attribute on the Test class. At that time, the function body isn't actually executed (as you might expect). However, it's worth noting that the function does not inherit class scope. The unadorned name 'x' references the global. If you want to access class-scope names from inside methods, you need to say 'self.x', which also applies to instance attributes. That's what would do what you expect here. ChrisA