Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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; 'model,': 0.05; 'tree': 0.05; 'subject:Python': 0.06; 'assignment': 0.07; 'explicit': 0.07; 'puts': 0.07; 'think,': 0.07; '22,': 0.09; 'global,': 0.09; 'skip:v 70': 0.09; 'subject:into': 0.09; 'way:': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '"global"': 0.16; '4:59': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals': 0.16; 'lookups.': 0.16; 'rules.': 0.16; 'subject:variable': 0.16; 'surprising': 0.16; 'travis': 0.16; 'variables,': 0.16; 'vary.': 0.16; 'sat,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; '(where': 0.19; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'case.': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'asked': 0.31; 'implicit': 0.31; 'allows': 0.31; 'quite': 0.32; 'worked': 0.33; 'there,': 0.34; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accessing': 0.36; 'keyword': 0.36; 'subject:?': 0.36; 'two': 0.37; 'handle': 0.38; 'rather': 0.38; 'subject:Can': 0.60; 'new': 0.61; "you're": 0.61; 'name': 0.63; 'refer': 0.63; 'different': 0.65; 'skip:\xe2 10': 0.65; 'default': 0.69; 'to,': 0.72; 'lack': 0.78; 'glad': 0.83; 'magical': 0.91; 'to:none': 0.92 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=3pEm9JCM1OdjeQGsOK2F4RwsI1s9KnrQMJhjyRRbG1E=; b=Jm3HI6sCG5XEG8N/7M1BzfKThd4fxLvEPRrxUKadQQxjXEYP52f397PqHmr4KSVycr 0K9+fPuC3X/cYdcet/Op9cdrRsj/LXlgXnqn/QhAV7aEMiYf5Crito9K0ernKH0L5U3V 3N+Eiy0ACCFu/RZsW6BYlngkViR1eJ3kQQvJjGd8d/ZK5SGSHV8YPlntklcmThsJbdd6 bPz9UPLFhHX5inAiV/tUGxXgcKhjGkSf+A+d5j9029nKqwyTDXsH1wcBUfK6azuCV4y0 ZkX/dROlLGBimPtkCaeRElB0K2DPxaQ7wO5D7OifKwGBCUm+MmByVyyQMubAPVUzDAn5 4SXA== MIME-Version: 1.0 X-Received: by 10.66.160.2 with SMTP id xg2mr10603424pab.23.1393006619981; Fri, 21 Feb 2014 10:16:59 -0800 (PST) In-Reply-To: <8454E8CB-E6E3-452F-8E54-9A77BFF34EC2@gmail.com> References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> <87sird7wuw.fsf@handshake.de> <8454E8CB-E6E3-452F-8E54-9A77BFF34EC2@gmail.com> Date: Sat, 22 Feb 2014 05:16:59 +1100 Subject: Re: Can global variable be passed into Python function? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393006629 news.xs4all.nl 2849 [2001:888:2000:d::a6]:42226 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66839 On Sat, Feb 22, 2014 at 4:59 AM, Travis Griggs wro= te: > What makes Python variables/bindings/references/aliases/namedvalues/slots= /bucketsofstuff surprising to new arrivals from other language kingdoms, is= that accessing is pragmatically implicit (walks the scope tree for you) an= d assignment may require explicitness. IOW, for some =E2=80=9Cvariables=E2= =80=9D, you have to do something explicit to make the variable you want to = refer to, vary. Some might say there is a lack of symmetry. Pros and cons. > What you're looking at there, I think, is actually quite tangential to the question of Python having/not having variables, and it's to do with scoping rules. Correct me if I'm wrong. g =3D "global" c =3D 0 def f(): print(g) # References two globals l =3D "local" print(l) # References one global and one local global c c =3D 1 # References one global As an alternative, Python could have asked us to declare the other way: def f(): print(g) # References two globals local l l =3D "local" print(l) # References one global and one local c =3D 1 # References one global It would still be the same name binding / object reference model, with just a different keyword used to change the default behaviour of name lookups. And the difference is comparatively slight. We get an encouragement to use locals rather than globals, but on the flip side, we need two keywords (global/nonlocal) where a single one (local, or as ECMAScript puts it, "var") could handle the other case. It's a lack of symmetry, but having worked with PHP (where you have to declare *every* global, except for the magical superglobals, and except for functions (which are in a completely different namespace), and except for constants), I am very much glad that Python puts everything in together as just "names", and then allows us to reference global names like "print" without declarations. It's a miswart. ChrisA