Mutable and Immutable

I am a beginner in C++ as well as Python. I am having a hard time understanding mutable and immutable objects in Python. According to the information a gathered from sources, immutable objects in Python such as int, float, and string, can't be modified once instantiated.

1
2
3
4
5
6
7
8
9
10
11
>>> number = 0  
>>> for i in range(5):
	print(number,id(number))  # This code indicates the id of number changes
	number += 1                     # as number is reassigned. Essentially, this is 


0 505910848                       # saying that each integer in Python has a
1 505910864                       # unique address and an id associated with it. 
2 505910880                       # So when number is reassigned. It points to
3 505910896                       # a different location in memory.
4 505910912



1
2
3
4
5
6
7
8
9
10
11
>>> my_string = "Hello"
>>> for i in range(5):
	print("{}, {}".format(my_string,id(my_string)))
	my_string += "1"

	
Hello, 49097568                   # The string class in Python is not mutable,
Hello1, 48989152                  # but according to the this code the string
Hello11, 48989152                 # "Hello1" and "Hello11" share the same id.
Hello111, 49109216                # How could this be?
Hello1111, 49109216



In C++, is it safe to say that all object are mutable? Because once an object is
declared and associated with a variable, the address of that object doesn't change even though the content of the object is modified.
Last edited on
no, you should read this:

https://www.geeksforgeeks.org/c-mutable-keyword/

c++ does let you change the address of things. It depends on what exactly you are doing. For example, read up on iterator invalidation in containers ... the container can drop and get different blocks of internal memory, breaking any old pointers... and you can do that yourself if you want to use pointers...
Last edited on
> In C++, is it safe to say that all object are mutable?

In general, non-const objects are mutable; objects of const-qualified types are immutable.

1
2
3
4
5
double d = 2.37 ; // mutable
std::string str = "hello" ; // mutable

const double cd = 2.37 ; // immutable
const std::string cstr = "hello" ; // immutable 


In addition, objects can't be modified when accessed through a const qualified path.

More information: https://isocpp.org/wiki/faq/const-correctness
You should note that the concepts surrounding immutability are different between interpreted languages and procedural constructs.

An interpreted language manages variable lifetime for you (typically). Immutability is a consequence of reference invalidation, or, more correctly, preventing it. Making things immutable means that objects can be multiply-referenced without unwanted consequences elsewhere, and makes reasoning about program correctness significantly easier (ie, not NP hard).

If we wish to change an object, we rebuild a new copy in memory with the desired modification. With our immutability constraints the is very efficient.

In procedural contexts, like C++, we handle objects entirely differently. In this case, immutability isn't really a concept. Instead we use the idea of "const correctness", which is aimed more at the programmer to enforce a do-not-modify guarantee.

Of course, we can break that guarantee any time we like, but doing so is likely to cause you trouble (and fail a code review) and typically highlights a design failure.

Sorry to be so brief; I'm on my mobile.
Topic archived. No new replies allowed.