2

If I define

foo = [[1,2],[2,3],[3,4]]
bar = foo

then foo and bar reference the same object, namely [[1,2],[2,3],[3,4]]. I can now use either of these "tags/namespaces/references" to make changes to the object [[1,2],[2,3],[3,4]], but how is this useful to anyone?

miku
  • 181,842
  • 47
  • 306
  • 310
John
  • 311
  • 1
  • 10
  • 6
    How is it *not* useful? *Everything* in Python is a reference. List entries are references. Calling a function creates new references. Attributes are references. **You are just not yet aware of this**. – Martijn Pieters Sep 07 '13 at 14:02
  • a should have been foo. I am very sorry for my mistake – John Sep 07 '13 at 14:04
  • The fact that you're asking about a list of lists makes the question "smell funny". Are you asking about the mutability of the *outer* list or the *inner* ones? IOW, is `bar=foo` more interesting or the fact that `bar=foo[:]` creates a shallow copy such that even now, `bar[0][1]=5` changes `foo[0][1]` *even though foo and bar are different lists*? – kojiro Sep 07 '13 at 14:38
  • @kojiro How are bar and foo different list in your example? You can not make any changes (no matter the depth) to the object that bar references that will not reflect in changes to the object that foo references. They will forever be indistinguishable. – John Sep 07 '13 at 15:01
  • @John First of all, they reside at different memory addresses (assuming CPython here). Try `id(foo)` vs `id(foo[:])`. Secondly, you can certainly change the items referenced by the outer lists without cross-contamination. Try `foo[:][2]=[3,2]`, then print `foo`. It's the *inner* lists that are identical, and that is what obscures your question. – kojiro Sep 07 '13 at 15:06
  • @kojiro `foo[:][2]=[3,2]` has no effect on `foo` or `bar`. However I see your point. – John Sep 07 '13 at 15:28

3 Answers3

2

One reason it can be useful to rebind names to existing values is if you intend to reuse the original name for a different object. For instance, this function to calculate the nth number in the Fibonacci sequence reuses the names a, b and temp repeatedly in a loop, binding the value previously referenced by a to b (via temp) each time:

def fib(n):
    a = 1
    b = 0
    for _ in range(n):
       temp = a
       a = a+b
       b = temp
       # A more Pythonic version of the last three lines would be: a, b = a+b, a
    return b
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • I do not think this applies to my question because integers are immutable object, while list are mutable object – John Sep 07 '13 at 14:38
  • @John: It's true that integers are immutable and the lists you gave as an example in your question are not, but I don't think that's a critical issue to your question of why you'd want to bind a new name to an already existing object with something like `a = b`. Whether the objects are mutable or not, a common reason for doing such an assignment is because you're later going to bind `b` to a different object but you still want a reference to its old value. – Blckknght Sep 07 '13 at 17:00
1

Let's say I have an attribute of my class instance, frobnoz, a reference to an instance of a Frobnoz class which in turn an attribute marfoos which is a list of all the associated marfoos, and I want to perform several operations on the first one.

marfoo = self.frobnoz.marfoos[0]
marfoo.rotate(CLOCKWISE, degrees=90)
if marfoo.is_cloudy():
    self.purge_clouds(marfoo)

If it were not possible to create an additional reference to the marfoo I wanted to perform actions on, I would have had to not only have lengthy references to it, but would also incur the expense of looking up both the frobnoz and marfoos references plus the first element of the list each time I wanted to use it.

Mattie
  • 20,280
  • 7
  • 36
  • 54
0

It is useful (among other things) for function calls that modify a value:

def changeFoo(bar):      
    bar[0][0]=3.14

changeFoo(foo)

Note: This does not technically use assignment, but it is equivalent.

It can also be used when multiple objects need to have a reference to the same object (such as in linked lists).

Flight Odyssey
  • 2,267
  • 18
  • 25