Nested function scope problem

Slawomir Nowaczyk slawomir.nowaczyk.847 at student.lu.se
Thu Aug 3 09:57:22 EDT 2006


On Sun, 30 Jul 2006 11:18:10 -0300
Gerhard Fiedler <gelists at gmail.com> 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:

1244896 3
1244896 3
1244888 4

#> 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.

#> You also don't expect the "identity" of a and b to be the same
#> after assigning one to the other.

Don't I?

-- 
 Best wishes,
   Slawomir Nowaczyk
     ( Slawomir.Nowaczyk at cs.lth.se )

You can tell a bigot, but you can't tell him much.




More information about the Python-list mailing list