Can global variable be passed into Python function?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 22 20:39:18 EST 2014


On Sat, 22 Feb 2014 14:15:22 +0000, Mark Lawrence wrote:

> On 22/02/2014 02:47, Dennis Lee Bieber wrote:
>> 	BASIC, C, FORTRAN, COBOL, Assembly... A "variable" is synonym for 
>>      an address [a box that holds things].
>>
>>
> In C.
> 
> int xyz = 1;
> 
> xyz is placed in a register.  What is xyz called now as it's not in
> memory?

Of course it is in memory, just not main memory, and it is still accessed 
via an address. It's just that the address is something equivalent to 
"Register 5" instead of "address 12345678 in RAM".

You're focusing on the wrong thing here. The distinction is not "in main 
memory" versus "in a register" (or somewhere else). The distinction is 
not *where* the value lives, but the semantics of what it means to 
associate a name with a value.

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


In Python-style languages, what we might call the "name binding" style of 
variables, that same xyz = 1 means:

- find or create the object 1
- associate the name 'xyz' with that object


In implementations like Jython and IronPython, the object is even free to 
move in memory while in use. But that's not the only difference. The big 
difference is that in "fixed location" languages, it makes sense to talk 
about the address of a *variable*. In C, you might ask for &xyz and get 
123456 regardless of whether xyz is assigned the value 1, or 23, or 999. 
But in Python, you can't ask for the address of a variable, only of the 
address of an *object* (and even that is useless to you, as you can't do 
anything with that address).




-- 
Steven



More information about the Python-list mailing list