references/addrresses in imperative languages

Andrea Griffini agriff at tin.it
Mon Jun 20 19:14:59 EDT 2005


On Sun, 19 Jun 2005 22:25:13 -0500, Terry Hancock
<hancock at anansispaceworks.com> wrote:

>> PS is there any difference between
>> t=t+[li]
>> t.append(li)
>
>No, but

Yes, a big one. In the first you're creating a new list
and binding the name t to it, in the second you're extending
a list by adding one more element at the end.
To see the difference:

  >>> a = [1,2,3]
  >>> b = a
  >>> a = a + [4]
  >>> print a
  [1, 2, 3, 4]
  >>> print b
  [1, 2, 3]
  >>>
  >>> a = [1,2,3]
  >>> b = a
  >>> a.append(4)
  >>> print a
  [1, 2, 3, 4]
  >>> print b
  [1, 2, 3, 4]
  >>>

Andrea



More information about the Python-list mailing list