Page 152 -
P. 152
no dumb questiohns
Q: I’m intrigued. When you stripped the line_spoken data Q: But surely Python can work out how many variables are
of unwanted whitespace, you assigned the result back to the referring to any one particular string?
line_spoken variable. Surely invoking the strip() method on
line_spoken changed the string it refers to? A: It does, but only for the purposes of garbage collection. If you
have a line of code like print('Flying Circus'), the
A: No, that’s not what happens. Strings in Python are immutable, string is not referred to by a variable (so any variable reference
which means that once a string is created, it cannot be changed. counting that’s going on isn’t going to count it) but is still a valid string
Q: But you did change the line_spoken string by removing object (which might be referred to by a variable) and it cannot have
its data changed under any circumstances.
Q: So Python variables don’t actually contain the data
any unwanted whitespace, right?
A: Yes and no. What actually happens is that invoking the assigned to them?
strip() method on the line_spoken string creates a
new string with leading and trailing whitespace removed. The new A: That’s correct. Python variables contain a reference to a
string is then assigned to line_spoken, replacing the data that data object.The data object contains the data and, because you
was referred to before. In effect, it is as if you changed line_ can conceivably have a string object used in many different places
spoken, when you’ve actually completely replaced the data it throughout your code, it is safest to make all strings immutable so
refers to. that no nasty side effects occur.
Q: Q: Isn’t it a huge pain not being able to adjust strings “in
So what happens to the replaced data?
place”?
A: Python’s built-in memory management technology reclaims the
RAM it was using and makes it available to your program. That is, A: No, not really. Once you get used to how strings work, it
unless some other Python data object is also referring to the string. becomes less of an issue. In practice, you’ll find that this issue rarely
trips you up.
Q: What? I don’t get it. Q:
A: It is conceivable that another data object is referring to the Are any other Python data types immutable?
string referred to by line_spoken. For example, let’s assume A: Yes, a few. There’s the tuple, which is an immutable list. Also,
you have some code that contains two variables that refer to the all of the number types are immutable.
same string, namely “Flying Circus.” You then decide that one of Q:
the variables needs to be in all UPPERCASE, so you invoke the Other than learning which is which, how will I know when
upper() method on it. The Python interperter takes a copy of the something is immutable?
string, converts it to uppercase, and returns it to you. You can then
assign the uppercase data back to the variable that used to refer to A: Don’t worry: you’ll know. If you try to change an immutable
the original data. value, Python raises a TypeError exception.
Q: And the original data cannot change, because there’s Q:
Of course: an exception occurs. They’re everywhere in
another variable referring to it? Python, aren’t they?
A: Precisely. That’s why strings are immutable, because you never A: Yes. Exceptions make the world go ’round.
know what other variables are referring to any particular string.
116 Chapter 4