Can global variable be passed into Python function?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Feb 23 01:20:41 EST 2014


On Sun, 23 Feb 2014 12:50:26 +1100, Chris Angelico wrote:

> On Sun, Feb 23, 2014 at 12:39 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> In C or Pascal-style languages, what we might call the "fixed address"
>> style of variables, a variable assignment like xyz = 1 does something
>> like this:
>>
>> - associate the name 'xyz' with some fixed location 
>> - stuff the value 1 into that location
> 
> Kinda. In its purest sense, C is like that. When you declare "int xyz;",
> the compiler allocates one machine word of space either in the data
> segment (if that's at top level, or is declared static) or on the stack
> (if it's local), and records that the name xyz points there. But an
> optimizing C compiler is allowed to do what it likes, as long as it
> maintains that name binding... and as long as any recorded address of it
> remains valid.


I think that's a red herring. The semantics of the language are that it 
behaves as if the variable had a fixed location. What goes on behind the 
scenes is interesting but fundamentally irrelevant if you want to 
understand the language itself: the compiler's implementation may give 
that variable a fixed location, or not, or multiple locations, or non-
fixed, or whatever it finds useful at the time, so long as the C code you 
write can assume that it is fixed. (That's like Python, which has no 
pointers. The fact that the implementation of some Python interpreters 
uses pointers all over the place is strictly irrelevant.)

In practice, because C compilers usually work so close to the metal, and 
programmers expect there to be a very direct correspondence between the C 
code you write and the machine code you get, the compiler is unlikely to 
simulate fixed addresses unless there is real benefit to be gained, it is 
more likely to actually use fixed addresses. But in principle, one might 
write a C interpreter in Jython, and simulate fixed addresses over the 
top of a language without any addresses at all (Python), which in turn is 
written in a language where the garbage collector can move things around 
(Java).

The important thing here is not so much that we are disagreeing, but that 
we are talking about two different levels of explanation. ("Gödel, Escher 
And Bach" has an very interesting section about how explanations at 
different levels can be radically different and even contradict each 
other.) At the C source code level, the language mandates that variables 
have fixed addresses, to the extent that C source code knows about 
addresses at all. At the generated machine code level, the compiler is 
free to play tricks if necessary.

Another example: if you have a variable unused=23, and the compiler 
removes it because it's dead code, we wouldn't argue that therefore C 
variables have no existence at all. Would we? :-)


> So, these days, C is becoming more like Python.

*raises eyebrow*

I think the stress of the exception-expression PEP is getting to you. 
They truly aren't.

*wink*



-- 
Steven



More information about the Python-list mailing list