Can global variable be passed into Python function?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Feb 23 11:30:34 CET 2014


On Sun, 23 Feb 2014 11:52:05 +0200, Marko Rauhamaa wrote:

> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> 
>> The big difference is that in "fixed location" languages, it makes
>> sense to talk about the address of a *variable*.
> 
> The address could be a symbol, too.
> 
> The Python statement
> 
>    xyz = 3
> 
> places a number in the address "xyz".

It doesn't matter whether addresses are numeric or symbolic, Python does 
not use that model for variables. Consider this example. There are a few 
steps, so let's go through them. First we prove that so-called address 
"abc" and "xyz" are distinct. If they were the same address, then they 
would logically have to contain the same value, but that is not the case:

abc = 23
xyz = 42
assert abc != xyz


This proves that the two addresses are different.

Now we prove that they are the same address:

abc = xyz = []
xyz.append(42)
assert abc == [42]


If they were different, then modifying the object at xyz cannot modify 
the object at abc. So we have a contradiction: "addresses" abc and xyz 
are both the same address, and different addresses.

There is a way to wiggle out of the contradiction: accept that the 
addresses are distinct, but claim that a single object can be in two 
different locations at once. But if we make that argument, we are 
stretching the metaphor of location quite considerably. Absent time-
travel, objects cannot be in two locations at once. Claiming that they 
can be requires stretching the metaphor of location past breaking point.

Self-confession time: some years ago, I used to make exactly that 
argument. I used to argue that the right way to visualise Python's 
variable model was to consider that objects were stored in variables in 
exactly the way you are suggesting. I didn't describe them as symbolic 
addresses, but otherwise the model was the same. In order to make the 
model work, I argued that objects could be in more than one place at 
once, including *inside* itself:

L = []
L.append(L)

and explicitly argued that this didn't matter. After all, if I can watch 
Doctor Who and accept the concept of his TARDIS materialising inside 
itself, I can understand the idea of a list being inside itself.

That was before I really grasped the difference between the name binding 
and fixed location variable models. While I'm still fond of the concept 
of a box being inside itself, I've come to understand that having to 
stretch the metaphor of location so far simply indicates that the 
metaphor does not work with Python's semantics.

Python's actual behaviour is not a good fit for the "variables are 
locations" model, not even if you think of locations in the abstract with 
symbolic names rather the numeric addresses.



-- 
Steven



More information about the Python-list mailing list