Values and objects

Chris Angelico rosuav at gmail.com
Sat May 10 03:21:56 EDT 2014


On Sat, May 10, 2014 at 4:15 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Sat, 10 May 2014 12:33:28 +1000, Chris Angelico wrote:
>> 1) Passing them as parameters. You can pass a pointer to a variable,
>> which is effectively the same as passing a variable to a function.
>
> No it is not. It is nothing like passing a variable to a function. You
> are passing a pointer, which is itself a value. True, it is a value which
> you, the author, gives meaning as a pointer to a variable, but that need
> not be the case. It might be a pointer to a part of an array, or a
> record, or to some random address in memory, or a dangling pointer. C
> allows you to perform arithmetic on pointers, which means you can
> construct pointers to nothing in particular.

I think at this point it's arguable, in that you can get so close to
"passing a variable to a function" that it doesn't really matter about
the distinction. But as I explained further down, it really just shows
that "patch of memory" can be passed around, and that a variable can
be backed by such a patch of memory.

> Rather than *creating* patches of memory, malloc merely allocates it from
> pre-existing memory.

I disagree. On a modern system with memory management, malloc can grab
memory from the system, thus making it available to your process.
Sure, physical memory will normally have to have been installed in the
system, but conceptually you could have a malloc function that
actually freezes the program, asks the user to build a new computer
and turn it on, connects to a new service on that computer, and
allocates memory from there. As far as your program's concerned,
malloc actually does (attempt to) give you more room than you had.

> Python variables aren't first-class either, but in fact we can get a bit
> closer to first-class than either C or Pascal.
>
> Creating new variables is trivial. Since they don't need to be declared,
> you create a new variable just by assigning to it:
>
> try:
>     spam
> except NameError:
>     spam = 23

No no no, this is really creating them at compile time. If you do this
inside a function, the name has to be created as a local name before
the function begins execution.

> There are at least two other ways:
>
> globals()['spam'] = 23
> exec('spam = 23')

With exec, you can do anything "at run time". Does that mean that, in
languages with an exec action, absolutely everything is first-class?
I'm not sure that that counts. Maybe I'm wrong.

Subscript-assigning to globals() is actually creating new
(module-level) variables, though. And if Python allowed you to assign
to locals() inside a function, then I would accept that local function
variables can be created and destroyed at run time, but you can't, so
local variables aren't first-class.

ChrisA



More information about the Python-list mailing list