Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail 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; 'subject:Python': 0.05; 'compiler': 0.05; 'from:addr:yahoo.co.uk': 0.05; 'url:pipermail': 0.05; 'objects,': 0.07; 'only,': 0.07; '"a"': 0.09; 'constants.': 0.09; 'immutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subsequently': 0.09; 'subtle': 0.09; 'variables,': 0.09; 'python': 0.10; 'python.': 0.11; 'interpreter': 0.15; 'variables': 0.15; 'constructs': 0.16; 'declared,': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'url:tutor': 0.16; 'weird.': 0.16; 'wrote:': 0.16; 'language': 0.19; 'machine': 0.21; 'saying': 0.22; 'assign': 0.22; 'lawrence': 0.22; 'names.': 0.22; 'pascal': 0.22; 'variables.': 0.22; 'code.': 0.23; 'leave': 0.23; 'bit': 0.23; 'seems': 0.23; "haven't": 0.24; 'previously': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'appear': 0.26; 'header:X-Complaints-To:1': 0.26; 'point.': 0.27; 'define': 0.27; 'object,': 0.27; 'dictionary': 0.29; 'flying': 0.29; 'thread,': 0.29; 'objects': 0.29; "we're": 0.30; 'work.': 0.30; 'certainly': 0.30; 'checks': 0.30; 'convention': 0.30; 'language.': 0.32; 'statement': 0.32; 'url:python': 0.33; 'common': 0.33; 'michael': 0.33; 'usually': 0.33; 'traditional': 0.33; 'languages': 0.34; 'this?': 0.34; 'important.': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'received:org': 0.37; 'seem': 0.37; 'things': 0.38; 'doing': 0.38; 'names': 0.38; 'why': 0.39; 'goes': 0.39; 'does': 0.39; 'rather': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'called': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'yes': 0.62; 'here.': 0.62; 'charset:windows-1252': 0.62; 'course': 0.62; 'matter': 0.63; 'to,': 0.63; 'times': 0.63; 'our': 0.64; 'binding': 0.66; 'differences': 0.66; 'fundamental': 0.66; 'numerous': 0.66; 'past,': 0.66; 'here': 0.66; 'talking': 0.67; 'act': 0.67; 'secret': 0.72; 'special': 0.73; 'forth': 0.79; 'casting': 0.84; 'irrelevant': 0.84; 'loose': 0.84; 'mistaken': 0.84; 'pythonistas,': 0.84; 'from.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Python handles globals badly. Date: Sat, 12 Sep 2015 04:01:24 +0100 References: <14afe27e-0bd5-410f-8e64-0f31d496ebf2@googlegroups.com> <55F36B4C.9020007@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 195.147.66.69 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <55F36B4C.9020007@gmail.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: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1442027105 news.xs4all.nl 23818 [2001:888:2000:d::a6]:60747 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96387 On 12/09/2015 01:01, Michael Torrie wrote: > On 09/11/2015 03:50 PM, Skybuck Flying wrote: >> Something which python does not seem to do currently ?! >> >> So that's weird. >> >> I will leave it at that for now. > > Seems to me you have a completely mistaken understanding of how > variables work in Python. This is one of the reasons why I have said in > the past, erroneously, that Python does not have variables. It does of > course but not in the same way as C or Pascal. In those languages names > are source-code abstractions only, and irrelevant to the compiler and > machine code. C and Pascal define variables as boxes that can be > written to. Not so in Python. > > In Python most common objects are immutable. Meaning they can never > change or be overwritten. They are bound to names. This binding is > what makes names look like and act like traditional variables. > > The secret to understanding the global keyword is to understand how > Python namespaces work. The statement "a=5" does not assign a 5 to the > box called "a." Rather it binds the name "a" to the "5" object, which > is immutable and called into existence by the interpreter > implementation. Subsequently "a=6" disconnects a from the 5 object, > casting the 5 object loose to be reclaimed in some fashion that doesn't > matter at this point. "a" is then rebound to a new object, 6. > > When doing a look-up on a name, the interpreter first checks the local > scope's dictionary and if it does not find the name there there, goes to > the outer scope and so forth until you get to the module global > namespace. So we don't need any special keywords to do Pascal-style > constants. We just define them in the module and they work. Usually we > name them in all caps so we have a bit of a convention as to where they > come from. And yes we're talking about looking up strings in a > dictionary here. > > When binding a name to an object, the interpreter always binds a name in > the local namespace, unless the global keyword has been used previously > and then it goes right to the global namespace. As has been said > numerous times on this thread, how else would the interpreter do this? > There simply isn't any other way that makes sense. Certainly you haven't > made the case for it, seeing as you have some fundamental > misunderstandings about variables in Python. > > You keep saying things like "writing to a variable" or "declared > variables" which just don't apply to Python because that's not how > Python variables work. It may appear this way on the surface, but the > differences are subtle yet important. Namespaces are written to, not > variables, some objects can be mutated. Names are bound to objects, but > variables are not declared, as a name can be bound to an object of any type. > > Namespaces are powerful constructs that give Python much of its dynamic > nature and expressivity. Learn to use them! > My favourite analogy for Python names, the sticky note, here https://mail.python.org/pipermail/tutor/2006-October/049767.html -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence