Nested function scope problem

Gerhard Fiedler gelists at gmail.com
Tue Aug 1 10:12:31 EDT 2006


On 2006-07-31 23:52:07, danielx wrote:

>> You don't expect the "identity" of the variable b to change with a
>> simple assignment from a C/C++ point of view. You also don't expect the
>> "identity" of a and b to be the same after assigning one to the other.
>> You can create C++ classes that behave like that (you can implement
>> Python in C++ :), but that doesn't mean that you expect C++ language
>> constructs to behave like that.
> 
> I'm really not comfortable with C, but I disagree. What would you say
> about this program: [...]

"Identity of a variable" is probably even less defined than "variable" :)

What I meant is that in a C/C++ context, I'd fuzzily think about identity
of a variable as its location, the memory address. In this sense, you
change the identity of the content of the pointers, but not the identity of
the pointer variable itself. 

After the line "letterBomb = string;", both letterBomb and string still
have each their own "identity" (as in memory address). You copied the
content; which is in this case the address that was stored in string. It's
that level that you never get to in Python (normal Python programming, at
least, I think :).

I think it's probably pretty moot to compare the low level C concept of
variables with the higher level concept of variables that most scripting
languages have. There's a lot of built-in functionality behind the scenes
that C lacks. It of course can be simulated in C with certain assumptions,
but that's not the point. 

There's maybe a point in comparing Python variables to C pointers. But it
lacks in the respect that a C programmer is used to modify memory locations
through pointers. A Python programmer isn't used to modify memory locations
in the first place, and simple objects don't get modified at all. There's
no Python equivalent to "int*p=345; *p++;". (There's no need for that in
Python; I'm not saying this shows a superiority of C. It just shows a
different concept of what "variable" means.)

For me, the point of this discussion was that it makes sense to look at it
/differently/. Once you've done that, there's no problem in continuing to
use the (vaguely defined) term "variable". 

I admit that for me it was a surprise when I learned that simple numbers
are immutable objects, and I'm sure this wasn't the last time I'll think a
bit about the ramifications of this. 

Gerhard




More information about the Python-list mailing list