Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: a clarification on the "global" statement sought Date: Fri, 11 Mar 2016 20:29:47 +1100 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ukfLinFTEa6gD/yOtmLh9wT9WEOihx2dE7Zpy193YDFg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'assignment': 0.07; 'subject:skip:c 10': 0.07; 'undefined': 0.07; 'cc:addr:python-list': 0.09; 'definition,': 0.09; 'namespace': 0.09; 'python': 0.10; 'def': 0.13; 'variables': 0.15; '"python': 0.16; '2016': 0.16; 'builtins.': 0.16; 'certainty': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'used."': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'resolved': 0.18; 'variable': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; 'function,': 0.22; 'name;': 0.22; 'appears': 0.23; 'references': 0.23; 'header:In- Reply-To:1': 0.24; 'module': 0.25; 'fri,': 0.27; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'looks': 0.29; 'print': 0.30; 'run': 0.33; 'received:google.com': 0.35; 'fail': 0.35; 'i.e.': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'assigned': 0.36; 'subject:" ': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209': 0.38; 'end': 0.39; 'subject:the': 0.39; 'still': 0.40; 'caused': 0.61; 'to,': 0.63; 'within': 0.64; 'mar': 0.65; 'deals': 0.67; 'further,': 0.72; '100%': 0.72; 'smith': 0.76; 'chrisa': 0.84; 'local,': 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; bh=Y4w1qCUqPyQffjFgg2/0XBMUnLRI5/iX9EdGpADcNzA=; b=ROQNKeuZEDUyDsJoRPYuo0x1mXeooutx4KgkwTMSgsjirzto9T6i2rEfYLyp/oY9W1 Q/J4IA0Kh7BKXLKsL1U3Ya23DDH642fFgD3EqNcj/L2C8Vix+9WYAT4KG2D0cxjwTlTO DDczRxHUEahl/JU5VMx3wXS7LfbZLm6nCfSUCRgNycIhpsvyq4rBG9FJF6/ylM+flYef zL7QvTpbeTNu4fLjEf8Uybzg687tFO+5FUhAIetrt32s173HwwV50vqFsbthNPw4nxK8 4wbLKhL0iZ6mmIYgVfzZEhKdalmRVWRt2F6N6vkVpPWKQF0iNTjWrEmUvgq1wQvnK8K6 AtAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=Y4w1qCUqPyQffjFgg2/0XBMUnLRI5/iX9EdGpADcNzA=; b=gisxAsQSF9zfXh5+DB03mECeg6osqWWrAPrd5TUtJZ4wD1Quz+ZUHbi3k3L79kcLZ/ 0jVjPpiPNH01ix/EPMz5OLcWYZ6fBcaPolm/+A6JegiwijNv/h6MCkHs2bJY4ZGkeBYK jpePA92WlzKLHDrSD50INxkZCmkmn/4NBpUm3uxU2UnOa2QmV69YdWrFq8Yh5JPiniKe tK1SZehpcVT09iFC7QJ6gtfa6qcv3XKJrFGp99tV/YTHIHHp5Lezv9K3fMMimtpJgjcz 25xwLb1j2xM0MUQ6pqhJQB4i9evDCA2CAvrlWiwuDHZyy+pCCfCSdge36qMZvQeSt9n2 R20w== X-Gm-Message-State: AD7BkJJU2e//zsBV9mM+FeFJXZQCyasCynod4vOsf3J6q4f+wtHsuxctdzuYJP8JuSXh0ee+mlB9lLtZnep3pw== X-Received: by 10.107.138.75 with SMTP id m72mr8565326iod.31.1457688587855; Fri, 11 Mar 2016 01:29:47 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:104603 On Fri, Mar 11, 2016 at 8:09 PM, Charles T. Smith wrote: > "Python deals with variables the other way around. > They are local, if not otherwise declared. > ... > def f(): > print(s) > s = "I love Paris in the summer!" > f() > ... > As there is no local variable s, i.e. no assignment to s, the value > from the ***global*** variable s will be used." > > Indeed "maverick": that a variable can be an undefined global > and then only appears as such when assigned to, has caused me > no end of grief. Looking purely at the function definition, you can see with 100% certainty that it references two non-local names: "print" and "s". Neither is assigned to within the function, so both are looked up externally. At run time, s is resolved as a module-level name; print is not, so Python looks further, to the builtins. Had s not been assigned to, it would still be a global name in the function, but it would fail to be found in either the module namespace or the builtins, and would result in NameError. But either way, it's a global name, whether it's assigned to or not. ChrisA