list storing variables

Peter Otten __peter__ at web.de
Mon Feb 23 13:41:23 EST 2015


Marko Rauhamaa wrote:

> Peter Pearson <pkpearson at nowhere.invalid>:
> 
>>>>>> a = 2; b = 5
>>>>>> Li = [a, b]
>>>>>> Li
>>> [2, 5]
>>>>>> a=3
>>>>>> Li
>>> [2, 5]
>>
>> [...]
>>
>> The word "variable" brings implications and assumptions that get
>> people into trouble in Python.  Python doesn't have variables.
>> It has objects, and you can assign names to objects -- like sticky
>> notes, as someone in this newsgroup helpfully pointed out long ago.
>>
>> This way of thinking is significantly different from the familiar
>> "variable" mind-set of C, but looks similar enough to produce some
>> confusion.  For me, the sticky-note analogy helped a lot, and in a
>> few days many things made more sense.
> 
> It's no different in C:
> 
>    #include <stdio.h>
> 
>    int main()
>    {
>       int a = 2, b = 5;
>       int Li[2] = { a, b };
>       printf("%d %d\n", Li[0], Li[1]);
>       a = 3;
>       printf("%d %d\n", Li[0], Li[1]);
>       return 0;
>    }
> 
> Outputs:
> 
>    2 5
>    2 5
> 
> 
> Marko

The OP explicitly mentions the & operator. There's no python analog to that 
and the behavior shown below:

$ cat pointers.c
#include <stdio.h>

int main()
{
  int a = 2, b = 5;
  int * Li[2] = { &a, &b };
  printf("%d %d\n", *Li[0], *Li[1]);
  a = 3;
  printf("%d %d\n", *Li[0], *Li[1]);
  return 0;
}
$ gcc pointers.c
$ ./a.out 
2 5
3 5





More information about the Python-list mailing list