Nested function scope problem
Gerhard Fiedler
gelists at gmail.com
Thu Aug 3 16:27:26 EDT 2006
On 2006-08-03 10:57:22, Slawomir Nowaczyk wrote:
> #> In any case, the following doesn't seem to be implementation detail
> #> (and rather a part of the language), but it's not really
> #> understandable with a C++ concept of "variable":
> #>
> #> >>> a=3
> #> >>> id(a)
> #> 3368152
> #> >>> b=a
> #> >>> id(b)
> #> 3368152
> #> >>> b=4
> #> >>> id(b)
> #> 3368140
>
> How about that?
>
> int main()
> {
> int three = 3;
> int four = 4;
> int *a, *b;
> a = &three;
> printf("%i %i\n",a,*a);
> b = a;
> printf("%i %i\n",b,*b);
> b = &four;
> printf("%i %i\n",b,*b);
> return 0;
> }
>
> Just in case you don't have C compiler at hand, it prints:
I don't have to :)
But seriously, for my comment this seems off-topic. I did not say that you
can't create Python behavior with C (of course you can, you can do
/everything/ in C :). You can build constructs made up of C variables that
simulate everything that any Python construct does. That's not the point.
The point is how the simple, built-in language variable behaves.
> #> You don't expect the "identity" of the variable b to change
> #> with a simple assignment from a C/C++ point of view.
>
> That depends on your definition of "identity", of course.
Right. "Identity" is not defined in C, but most C programmers would
probably take the address of a variable as the closest to a variable's
identity.
> #> You also don't expect the "identity" of a and b to be the same
> #> after assigning one to the other.
>
> Don't I?
I don't know. Try replacing your printf statements with something like
"printf("%x %i %i\n",&a,a,*a);" and watch the first column. The address
operator is probably for a C programmer the closest to what the id()
function is to a Python programmer.
Gerhard
More information about the Python-list
mailing list