Reputation: 279
Does an immutable object in python mean that its value cannot be changed after its conception? If that is the case what will happen when we try to change its value.
Let me try to explain my doubt with an example.
For instance I initialized a String object S
with value "Hello World"
.
S = 'Hello World'
Then I typed in the line,
S = 'Hello Human'
So when I ask the interpreter, it tells me the value of S is "Hello Human"
. Clearly now 'S' has a new value.
How did the value change? Did python destroy the old string object and created a new one with the new value? or did it just changed the value of the old object. Does this has anything to do with the fact that the string object is immutable? If so, then how do mutable objects behave?
Upvotes: 5
Views: 1881
Reputation: 61498
Does an immutable object in python mean that its value cannot be changed after its conception?
Yes.
If that is the case what will happen when we try to change its value.
The question is invalid, because you never try to change its value, because there is no means available to change try.
What you are really asking is what happens when we assign to a variable that names the value.
I want you to take special note of the word "a" there: more than one variable can name the same value. I also want you to take special not of the word "names": Python variables are indeed names - they do not store values, but provide names for them.
Clearly now 'S' has a new value.
Now S
names a different value.
How did the value change?
It didn't. S
ceased to name what it used to name, and started to name a new value.
Did python destroy the old string object
It may have, if there are no other names for it. If there are no names for a value, it will get destroyed eventually. That is not your concern as the programmer.
and created a new one with the new value?
Yes.
If so, then how do mutable objects behave?
The same way, when you assign to the name. Names are names, and values are values. (In this context, "object" and "value" mean the same thing.) Assignment is the process of causing a name to name a value. It doesn't matter if the object is mutable or not. Mutation is done in other ways. (Although you could make an argument that, for example, a list
is really a list of names, and you don't really mutate the list, but instead change the quantity of names and their referents.)
Upvotes: 1
Reputation: 11120
Variables in python are nothing more than bindings, so when you use the assignment operator you are either creating a new binding or replacing a previous depending on which version of python you may be using ... But I think this are implementation details you don't have worry about.
Mutable objects such as dictionaries/lists can change their internal state through various methods, either internal or external, immutable objects have no such methods, their state can only be set during construction and the objects themselves can only be destroyed unless they are pooled for performance reasons ...
Upvotes: 0
Reputation: 17076
So if you're doing something like this:
>>> s = "Hello World"
>>> print s
"Hello World"
>>> old_s = s
>>> s = "Hello Human"
>>> print s
"Hello Human"
>>> print old_s
"Hello World"
You can see that strings in Python aren't mutable (literally--you can't mutate them).
When you add to a string (or to other immutable objects, like tuples or integers), a new copy of the object is created and the reference to the old one is deleted. (Python handles garbage collection for you, when nothing is referencing the object any more).
Upvotes: 4
Reputation: 2770
s = 'helloworld'
>>> id(s)
29779592
>>> s = 'hello'
>>> id(s)
29771680
Now the variable S refer to new object
Upvotes: 2
Reputation: 54079
Python stopped S
pointing to the the old string object and made it point to a new one
>>> S="Hello World"
>>> id(S)
32386960
>>> S="Hello Human"
>>> id(S)
32387008
>>>
You can't change immutable objects, so even when you think you muight be (eg with the +=
operator) you aren't
>>> S="Hello"
>>> id(S)
32386912
>>> S+=" World"
>>> id(S)
32386960
>>>
Upvotes: 8
Reputation: 798546
The value did not change. The name was bound to a new object.
Upvotes: 4