Can global variable be passed into Python function?

Chris Angelico rosuav at gmail.com
Sun Feb 23 08:23:57 CET 2014


On Sun, Feb 23, 2014 at 5:20 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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.

I think that's a good summary, actually. C has variables, at the
source code level, but once you compile it down, it might not. From
which it follows that C variables have addresses (unless they're
declared 'register', in which case they might still be stored in RAM
but no longer have addresses), which may or may not have any actual
relationship to memory addresses.

> 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*

Haha. I wasn't joking, though; a fully-optimizing compiler is free to
do so much that, in reality, you're not writing bare-metal code -
you're writing guidelines to a sophisticated program generator. C has
become as high level a language as any other, and has acquired some
Python-like features (check out some of the recent standards); and,
the part that I was talking about here, it makes the most sense to
talk about "name bindings" rather than memory addresses. Variables
might move around, but if you say "int xyz=5;", then you can be sure
that querying xyz will give you back 5. The concreteness of "declare a
variable ergo memory is allocated for it" is no longer the case.

ChrisA



More information about the Python-list mailing list