variable in Python

Dan Bishop danb_83 at yahoo.com
Thu Apr 17 14:17:24 EDT 2003


Salvatore <artyprog at wanadoo.fr> wrote in message news:<b7lvbh$an3$1 at news-reader13.wanadoo.fr>...
> Hello,
> 
> I have a little doubt.
> when you declare x = 3

<nitpick>There are no variable declarations in Python.</nitpick>

> does the variable x 'contains' the value 3 (like in Pascal)
> or does it contains an address which points to
> a memory place containing the value 3 ?

The latter: All variables in Python are pointers in disguise.

This is why, after executing

list1 = [0, 1, 2, 3]
list2 = list1
list2[3] = 0

list1 will be [0, 1, 2, 0] instead of [0, 1, 2, 3].  The assignment
"list2 = list1" copies a pointer to the list, not the contents of the
list.

Note that I used a list instead of an int.  With ints, the distinction
between value and reference semantics doesn't matter as much because
ints are immutable.




More information about the Python-list mailing list