Nested function scope problem

Gerhard Fiedler gelists at gmail.com
Fri Aug 4 09:10:45 EDT 2006


On 2006-08-04 07:36:25, Slawomir Nowaczyk wrote:

> #> The address operator is probably for a C programmer the closest to
> #> what the id() function is to a Python programmer.
> 
> I disagree. At least in my understanding, which, up to now, was
> perfectly enough to explain everything about how Python variables
> behave:
> 
> The address operator in C is what textual representation (i.e. what
> you type, like "a") is in Python. Equivalent of id() is a dereference
> operator.

But then you are not talking about C variables. Using a normal C variable,
this doesn't work:

  int c = 5;
  printf( "id(c)=%x", *c );

You can hardly claim that what gets printed is the "id" of the variable c.
(Well, you can claim, but few C programmers would follow you.)

What would be analogous in Python to the textual representation of the
variable in C? The address operator in C in analog to the textual
representation in Python. The textual representation in C is analog to ???
in Python?


You may be talking about C pointers and call the locations they point to
"variables". That's a big difference; those are /not/ C variables. And they
still are not the same.

What is analog in Python (using your analogy) to the address of the pointer
variable in C (&a in your example)?

Note that in standard C/C++ language, a and b in your example are variables
(in fact the only variables), not *a and *b. (three and four should have
been declared as constants, to be analogous to Python.) So the only
variables in your example (a and b) don't really behave according to your
analogy. What behaves somewhat like your analogy are *a and *b -- neither
of which are C/C++ variables. (Well, they are in your example, but only
because of sloppily applying your analogy. And they are not in the general
case: pointers don't care whether they point to actual C/C++ variables, or
to any other memory location.)

Gerhard




More information about the Python-list mailing list