1

I'm new in Python (new as in it's been 2 days since I first began learning about it) but I'm somewhat already experienced in C and I was learning about mutability and immutability when my professor told me that integers and other data types in Python are immutable. He showed "proof" of this by assigning an int to a variable, showing the memory address, modifying the variable and showing the memory address in it again which was modified.

I began experimenting with this, and found the following:

>>> a = 5
>>> id(a)
140708610844592 
>>> a = 6
>>> id(a)
140708610844624
>>> # jumps 32 bits
>>> a = 5
>>> id(a)
140708610844592
>>> #Same address 'a' declared the first time
>>> b = 5
>>> id(b)
140708610844592
>>> #Same address as a
>>> c = [3,4,5,6]
>>> id(c)
2168086594816
>>> id(c[2]) #index of 5
140708610844592
>>> #same address for 5 but memory not contiguous data type 'list' is a pointer?
>>> c = [3,4,5,6]
>>> id(c)
2168086406272
>>> #Even though list is the same, memory changes which is not happening for int
>>> x = 7
>>> id(x)
140708610844656
>>> 140708610844656 - 140708610844624 #address of 'x' minus address of when 'a' was 6
32
>>> # mem in 6 and 7 are contiguous
>>> id(8) #Value not declared before
140708610844688
>>> 140708610844688 - 140708610844656 #address of 8 minus address of x
32
>>> # x and 8 are contiguous, ints pre-declared?
>>> z = (3,4,5,6) #tuple
>>> id(z)
2168086643872
>>> id(z[2])
140708610844592 #Always same location for int 5
>>> z = (3,4,5,6)
>>> id(z)
2168086750288 #same tuple sequence, different address, same happened with list
>>> id(100)
140708610847632
>>> 140708610847632 - 140708610844592 #Address in 100 minus address in 5
3040
>>> 3040/32 #number of bits between size of int
95.0 # between 5 and 100 exist 95 consecutive ordered ints
>>> st = "hello"
>>> id(st)
2168086495280
>>> id(st[0]) #address of character 'h'
2168051747440
>>> st = "hell"
>>> id(st)
2168086498416
>>> id(st[0])
2168051747440
>>> st = "hello"
>>> id(st)
2168086498352
>>> id(st[0])
2168051747440 #address of 'h' remains the same but str 'hello' address different

So, based on this my questions are obvious.

Do integers and other characters already come predefined and variables are just assigned their memory address depending on these values?

Memory address for tuples, str, lists keep changing even when the same secuences are declared.

So from my point of view if we consider as immutable every data type that changes address when redefined, then practically every data type variable is immutable except for predefined integers and chars that basically do not change their location in memory.

Therefore a list containing int elements is as immutable as a tuple.

Or does mutability/immutability only refers to if the elements in a secuence can be modified or not, which in case an int it's neither mutable or not because it's an object itself, not a secuence.

And also, data types list, tuple, etc are just pointers pointing at other addresses? unlike arrays it does not seem that their elements are together but in different locations, like a linked list.

I hope you guys can help me clarify this.

Adolf
  • 11
  • 1
  • I'm not sure what your professor was trying to demonstrate. Simply put, **mutability is defined as an object that exposes mutator methods**. That is it. IOW "So from my point of view if we consider as immutable every data type that changes address when redefined" Is simply wrong. This is a red-herring. – juanpa.arrivillaga Jul 25 '21 at 07:22
  • As far as what you are seeing with addresses, you are seeing the result of various optimizations and implementation details that are *specific to CPython*. Namely, small `int` objects are cached,. – juanpa.arrivillaga Jul 25 '21 at 07:23
  • Whatever your python does memory wise is implementation specific. Integers are preallogcated in the area of -6 to 256 or so (https://stackoverflow.com/questions/15171695/whats-with-the-integer-cache-maintained-by-the-interpreter) - assuming how it works below the scripting surface does nothting for you and certainly does not help you get better in python – Patrick Artner Jul 25 '21 at 07:24
  • "And also, data types list, tuple, etc are just pointers pointing at other addresses? " **python doesn't have pointers**. Everything acts as a reference though, but there is not special behavior for `list`, `tuple` etc that is distinct from say, `int` or `str` – juanpa.arrivillaga Jul 25 '21 at 07:24
  • Fundamentally, stop trying to understand Python like you would try to understand C. The semantics are very different. And Python is a much higher level language. Namely, **everything is an object**. – juanpa.arrivillaga Jul 25 '21 at 07:26
  • "Therefore a list containing int elements is as immutable as a tuple." **no**. `x = [1, 2, 3]; x[0] = 99`. You have now mutated that list. The objects the list happens to contain **is completely irrelevant** – juanpa.arrivillaga Jul 25 '21 at 07:27

0 Answers0