Can global variable be passed into Python function?
rosuav at gmail.com
Sun Feb 23 02:50:26 CET 2014
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. It's actually very similar to what was discussed in
another thread recently about PyPy and the id() function - the
compiler's free to have xyz exist in different places, or not exist at
all, as long as the program can't tell the difference. I don't know
whether PyPy allocates an id for everything or only when you call
id(), but if the latter, then it's exactly the same as a C compiler
with the address-of operator - if you never take the address, it
doesn't have to have one (and even if you do, it's free to fiddle with
things, unless you declare the variable volatile).
So, these days, C is becoming more like Python.
More information about the Python-list