pointer

Don Garrett garrett at bgb-consulting.com
Tue Apr 9 13:00:23 EDT 2002


Andrey Panchenko wrote:
>> a = 1
> b = a
> a = 3
> 
> b must hold 3. But it's not true. IMHO it's better to say that the python
> names are like pointers only for mutable objects.
> (Caution - newbie opinion)
> 
> Andrey mailto:andrey at app.omsk.su

  The assignment a = 3 assigned a new reference, not a new value to an
existing integer.

  a = [1, 2]
  b = a
  a = [2, 3]

  Will leave b with the value [1, 2]. b referenced the same value as a, but
then a new reference was assigned to a, leaving b with the old value.

  BUT

  a = [1, 2]
  b = a
  a = a.append(3)

  Will leave b with the value [1, 2, 3], because b points to the same list as
a.

  All variables reference values, which is not quite the same thing as a
pointer. At least not exactly.

--
Don Garrett                             http://www.bgb.cc/garrett/
BGB Consulting                                      garrett at bgb.cc



More information about the Python-list mailing list