26

When I write this code:

polly = "alive"
palin = ["parrot", polly]
print(palin)
polly = "dead"
print(palin)

I thought it would output this:

"['parrot', 'alive']"
"['parrot', 'dead']"

However, it doesn't. It does output:

['parrot', 'alive']
['parrot', 'alive']

How do I get it to output that (the former)?

pippo1980
  • 2,181
  • 3
  • 14
  • 30
Flexo1515
  • 1,007
  • 1
  • 10
  • 27

6 Answers6

58

Python variables hold references to values. Thus, when you define the palin list, you pass in the value referenced by polly, not the variable itself.

You should imagine values as balloons, with variables being threads tied to those balloons. "alive" is a balloon, polly is just a thread to that balloon, and the palin list has a different thread tied to that same balloon. In python, a list is simply a series of threads, all numbered starting at 0.

What you do next is tie the polly string to a new balloon "dead", but the list is still holding on to the old thread tied to the "alive" balloon.

You can replace that thread to "alive" held by the list by reassigning the list by index to refer to each thread; in your example that's thread 1:

>>> palin[1] = polly
>>> palin
['parrot', 'dead']

Here I simply tied the palin[1] thread to the same thing polly is tied to, whatever that might be.

Note that any collection in python, such as dict, set, tuple, etc. are simply collections of threads too. Some of these can have their threads swapped out for different threads, such as lists and dicts, and that's what makes something in python "mutable".

Strings on the other hand, are not mutable. Once you define a string like "dead" or "alive", it's one balloon. You can tie it down with a thread (a variable, a list, or whatever), but you cannot replace letters inside of it. You can only tie that thread to a completely new string.

Most things in python can act like balloons. Integers, strings, lists, functions, instances, classes, all can be tied down to a variable, or tied into a container.

You may want to read Ned Batchelder's treatise on Python names too.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you, I believe now that I asked the incorrect question but thank you – Flexo1515 Aug 22 '12 at 20:13
  • 13
    I like the balloon analogy -- especially if they're helium balloons which float away when the last string is released... (+1). – mgilson Aug 22 '12 at 20:14
  • @user1404053: I updated some more to explain how strings are not mutable. You have to replace one string "balloon" with another. – Martijn Pieters Aug 22 '12 at 20:17
  • 4
    Does this mean garbage collection strategies correspond to weather? – DSM Aug 22 '12 at 20:18
  • 9
    @DSM: Exactly :-) GC is the wind that blows away any balloons not tied down. :-) – Martijn Pieters Aug 22 '12 at 20:19
  • @DSM -- I was more thinking that garbage collection strategies correspond to parties (or maybe just small children at the fair ...). However, meteorology always seemed like a party of an occupation to get into, so ... Tomato, tomato. – mgilson Aug 22 '12 at 20:20
  • Was more wondering how to change one variable so that multiple lists would reflect the change without needing to be changed individually – Flexo1515 Aug 22 '12 at 20:23
  • 1
    @user1404053: then you need a mutable balloon. A user-defined class, for example, instead of a string. – Martijn Pieters Aug 22 '12 at 20:23
  • @user1404053 mutable balloons ... I suppose those would be the long skinny ones that clowns can turn into other things (like dogs or hats). All joking aside, this is a problem that the `Tkinter` people ran into when designing the python-tk interface. They created helper classes `StringVar` and `IntVar` for this sort of purpose. They're very simple classes which have some data attribute and then you can just swap out what "balloon" that attribute is "tied" to. – mgilson Aug 22 '12 at 20:28
  • I replaced the strings with int ("alive" became 3 and "dead" became 4) and still no change, even when using palin[1] = polly – Flexo1515 Aug 22 '12 at 20:33
  • This is a great explanation. I would add that you could do something similar to what @user1404053 is asking using a user-defined class and you could have great control over how it was implemented by using properties ( http://docs.python.org/library/functions.html#property ). – TimothyAWiseman Aug 22 '12 at 21:52
  • Nice analogy for explaining valueType versus referenceType – dreftymac Apr 29 '17 at 09:00
4

Before your second print statement, store your new values into palin:

palin = ["parrot", polly]
girasquid
  • 15,121
  • 2
  • 48
  • 58
  • 1
    Anyway to do it without restoring the new value? – Flexo1515 Aug 22 '12 at 20:07
  • 3
    @user1404053: Not the way you're asking. In Python strings are immutable, so there's no way to change the value in `polly` and therefore change it everywhere else. The line `polly = "dead"` is a [rebinding operation rather than a mutating one](http://en.wikipedia.org/wiki/Name_binding#Rebinding_and_mutation) – David Robinson Aug 22 '12 at 20:11
4

When you put a string in a list, the list holds a copy of the string. It doesn't matter whether the string was originally a variable, a literal value, the result of a function call, or something else; by the time the list sees it, it's just a string value. Changing whatever generated the string later will never affect the list.

If you want to store a reference to a value that will notice when that value changes, the usual mechanism is to use a list containing the "referenced" value. Applying that to your example, you wind up with a nested list. Example:

polly = ["alive"]
palin = ["parrot", polly]
print(palin)
polly[0] = "dead"
print(palin)
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
2

The list will contain values only, not references to variables as you would like. You could however store a lambda in the list, and have the lambda look up the value of your variable.

>>> a = 'a'
>>> list = ['a',lambda: a]
>>> list[1]
<function <lambda> at 0x7feff71dc500>
>>> list[1]()
'a'
>>> a = 'b'
>>> list[1]()
'b'
Jonatan
  • 2,734
  • 2
  • 22
  • 33
  • Some more explanation: here `lambda: a` is basically a function that just returns the value stored in `a`. That's why you can use the command `list[1]()`; it means you call the function stored in `list[1]`. (Also, appropriate gravatar, @Jonatan) – Matthew Adams Aug 22 '12 at 20:17
  • Quite right, thanks for the explanation. Also I had completely forgotten my avatar, haha! – Jonatan Aug 22 '12 at 20:20
1

You can't. Assignment to a bare name is Python always only rebinds the name, and you cannot customize or monitor this operation.

What you can do is make polly a mutable object instead of a string, and mutate its value instead of rebinding the name. A simple example:

>>> polly = ['alive']
>>> items = ['parrot', polly]
>>> items
['parrot', ['alive']]
>>> polly[0] = 'dead'
>>> items
['parrot', ['dead']]
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
0

The other answers have explained what's going on well.

This is one of the (several) problems that motivate the use of objects. For example, one might do this:

class Animal:
    def __init__(self, aniType, name):
        self.aniType = aniType
        self.name = name
        self.isAlive = True

    def kill(self):
        self.isAlive = False

    def getName(self):
        return self.name

    def getType(self):
        return self.aniType

    def isLiving(self):
        return self.isAlive


polly = Animal("parrot", "polly")

print(polly.getName()+' the '+polly.getType()+' is alive?')
print(polly.isLiving())

polly.kill()

print(polly.getName()+' the '+polly.getType()+' is alive?')
print(polly.isLiving())

It may look like a lot of code at first for a simple task, but objects are often the way to go for things like this, because they help keep everything organized.

Here's the output of that program:

polly the parrot is alive?
True
polly the parrot is alive?
False
Matthew Adams
  • 9,426
  • 3
  • 27
  • 43