Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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; 'python,': 0.02; 'explicitly': 0.04; 'subject:Python': 0.05; 'compiler': 0.05; 'transform': 0.05; "'a'": 0.07; 'pypy': 0.07; 'undefined': 0.07; 'python': 0.09; 'correct,': 0.09; 'corresponds': 0.09; 'dict': 0.09; 'fashion.': 0.09; 'impose': 0.09; 'keyed': 0.09; 'namespace': 0.09; 'optimizing': 0.09; 'semantics': 0.09; 'variables,': 0.09; 'cc:addr:python-list': 0.10; 'language': 0.14; '(the': 0.15; '(meaning': 0.16; 'address).': 0.16; 'allocates': 0.16; 'bindings,': 0.16; 'both.': 0.16; 'compilation,': 0.16; 'describing': 0.16; 'invoking': 0.16; 'locations.': 0.16; 'question)': 0.16; 'ranges.': 0.16; 'remembers': 0.16; 'subject:Objects': 0.16; 'third,': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'uniquely': 0.16; 'variable.': 0.16; 'variables;': 0.16; 'later': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'compilation': 0.17; 'instance,': 0.17; 'pieces': 0.17; 'restrictions': 0.17; 'tries': 0.17; 'variables': 0.17; 'memory': 0.18; 'variable': 0.20; 'equivalent': 0.20; 'written': 0.20; 'latter': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'example': 0.23; '(you': 0.23; 'absolute': 0.23; 'split': 0.23; 'somewhere': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'compiled': 0.27; 'first,': 0.27; 'guess': 0.27; 'language.': 0.27; 'interface': 0.27; "doesn't": 0.28; 'fixed': 0.28; 'lines': 0.28; 'run': 0.28; 'closer': 0.29; 'consisting': 0.29; "d'aprano": 0.29; 'equivalent,': 0.29; 'second,': 0.29; 'steven': 0.29; 'wind': 0.29; 'source': 0.29; "i'm": 0.29; 'knows': 0.30; 'notes': 0.30; 'function': 0.30; 'expect': 0.31; 'code': 0.31; 'addresses': 0.32; 'could': 0.32; 'print': 0.32; "aren't": 0.33; 'traditional': 0.33; 'languages': 0.33; 'that,': 0.34; "can't": 0.34; 'program,': 0.34; 'along': 0.35; 'locations': 0.35; 'mapping': 0.35; 'stores': 0.35; 'received:192.168.0': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'anything': 0.36; 'address.': 0.36; 'possible': 0.37; 'optimization': 0.37; 'does': 0.37; 'two': 0.37; 'uses': 0.37; 'ones': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'sure': 0.38; 'takes': 0.39; 'received:192': 0.39; 'easily': 0.39; 'called': 0.39; 'little': 0.39; 'high': 0.61; 'share': 0.61; 'first': 0.61; 'free': 0.61; 'situation': 0.62; 'close': 0.63; 'mentioned': 0.63; 'different': 0.63; 'more': 0.63; 'behavior': 0.64; 'levels': 0.66; 'fact,': 0.69; 'ranges': 0.71; 'life.': 0.81; '"just': 0.84; 'disappear': 0.84; "it'd": 0.84; 'locals': 0.84; 'not...': 0.84; 'observed': 0.84; 'received:192.168.0.3': 0.84; 'technically': 0.91; 'imagine': 0.96 Date: Sun, 26 Aug 2012 00:45:55 -0500 From: Evan Driscoll User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: "Steven D'Aprano" Subject: Re: Re: Objects in Python References: <18409992-1e28-4721-8e64-60c69668da4e@googlegroups.com> <87d32i1ntc.fsf@benfinney.id.au> <7df4c317-7ad8-4158-900a-b52f19c3caf2@k9g2000pbr.googlegroups.com> <503750b9$0$6574$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <503750b9$0$6574$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345959962 news.xs4all.nl 6871 [2001:888:2000:d::a6]:60860 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27903 On 08/24/2012 05:00 AM, Steven D'Aprano wrote: > No. The compiler remembers the address of 'a' by keeping notes about it > somewhere in memory during the compilation process. When you run the > compiled program, there is no longer any reference to the name 'a'. > > ... > > The mapping of name:address is part of the *compilation* process -- the > compiler knows that variable 'x' corresponds to location 12345678, but > the compiled code has no concept of anything called 'x'. It only knows > about locations. The source code 'x = 42' is compiled into something like > 'store 42 into location 12345678'. (Locations may be absolute or > relative.) > > In languages with name bindings, the compiler doesn't need to track > name:address pairs. The compiled application knows about names, but not > about addresses. The source code 'x = 42' is compiled into something like > 'store 42 into the namespace using key "x"'. What you describe is sorta correct, but it's also not... you're describing implementations rather than the language. And while the language semantics certainly impose restrictions on the implementation, I think in this case the situation is closer than you acknowledge: From the Python side, I suspect that for most functions, you'd be able to create a Python implementation that behaves more like C, and allocates locals in a more traditional fashion. I don't know much about it, but I'd guess that PyPy already does something along this line; someone also mentioned that Cython (admittedly not a full-blown Python implementation, but close for the purpose of this question) tries to do the same thing. On the C side, imagine a function with locals x, y, and z which never takes the address of any of them. (You said later that "Just because the public interface of the language doesn't give you any way to view the fixed locations of variables, doesn't mean that variables cease to have fixed locations.") First, C variables may not even have a memory address. They can disappear completely during compilation, or live in a register for their entire life. Second, it's possible that those variables *don't* occupy a fixed location. If you never explicitly take an address of a variable (&x), then I can't think of any way that the address can be observed without invoking undefined behavior -- and this means the C compiler is free to transform it to anything that is equivalent under the C semantics. In particular, it can split uses of a variable into multiple ones if there are disjoint live ranges. For instance, in: x = 5 print x x = 10 print x there are two live ranges of x, one consisting of lines 1 and 2, and one consisting of lines 3 and 4. These live ranges could have been different variables; I could just of easily have written x = 5 print x y = 10 print y and these pieces of code are observationally equivalent, so the compiler is allowed to generate the same code for both. In particular, it could either compile the second example to share the same memory address for x and y (meaning that a memory address isn't uniquely named by a single variable) or it could compile the first to put the two live ranges of x into different memory addresses (meaning that a variable doesn't uniquely name a memory address). In fact, I'd *expect* an optimizing compiler to share memory for x and y, and I'd also expect to be able to concoct an example where different live ranges of one variable wind up at different addresses. (The latter I'm less sure of though, and I also expect it'd be a little hard, as you'd have to come up with an example where even at the high optimization levels you'd need to see that, both live ranges would wind up in memory.) Third, and more wackily, you could technically create a C implementation that works like Python, where it stores variables (whose addresses aren't taken) in a dict keyed by name, and generates code that on a variable access looks up the value by accessing that dict using the name of the variable. Evan