0
a=1
b=a

b += 1

When I increment b by 1, it points to a different location in the memory. Therefore, a will still be 1 while b becomes 2. However when I create my own integer class

class integer:
    def __init__(self, integer):
        self.integer= integer
    

and create two integer objects such as

a= integer(1)
b= a

and increment b by 1

b.integer+= 1

b will still point to the same memory location with a and both a and b will be 2.

Why does this happen like that? Since everything is an object in python, shouldn't both case1 and2 behave the same way?

Emre Uğur
  • 71
  • 1
  • 6
  • 1
    `b=a` **never** copies data. You currently have two references to the same object. The difference comes down to what you do *after*. `b += 1` *creates a new `int` that is the result of `b + 1` and re-assigns it to `b`*. So the `int` object being referred to by `b` is not mutated, hence `a` won't see any difference. `int` objects are immutable, they don't expose mutator methods. In the case of `integer`, `b.integer += 1` assigns the result of `b.integer + 1` back to `b.integer`, which *mutates the object*, hence, the change is visible in `a`, since they are still referring to the same object. – juanpa.arrivillaga May 13 '21 at 20:12
  • 1
    "Why does this happen like that? Since everything is an object in python, shouldn't both case1 and2 behave the same way?" No, they shouldn't, since you are doing two completely different things. Anyway, you should read: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga May 13 '21 at 20:14
  • @juanpa said it best, but this may also help. Use Python's `id` function to see the memory location of an object (example, `id(a)`). After doing this you'll see that initially `a` and `b` refer to the same memory location, which I believe is for optimization. However, when you modify one of those variables then it will create a new object and update the variable reference. – h0r53 May 13 '21 at 20:14
  • Related: https://stackoverflow.com/questions/8056130/immutable-vs-mutable-types – Pranav Hosangadi May 13 '21 at 20:14
  • 1
    @h0r53 `id` to be pedantic, the `id` function returns *a unique number* that is guaranteed to be unique for the lifetime of the object. The fact that in CPython, this is the address of the PyObject header is an *implementation detail*. – juanpa.arrivillaga May 13 '21 at 20:15
  • 1
    @h0r53 also, `a = b` **will always result in two references to the same object**. This isn't some memory optimization, this is a fundamental aspect of the semantics of the language. – juanpa.arrivillaga May 13 '21 at 20:17
  • FYI, be careful with numbers between 0 and 2^8. CPython creates this numbers at startup for optimizing because we often need small numbers (loop, flag, ...). So `a = b = c = d = e = 12` references always id(12). `a += 1` references id(13) and so on. – Corralien May 13 '21 at 20:33
  • @Corralien yes, although it's numbers [-5, 256] – juanpa.arrivillaga May 13 '21 at 20:34
  • I knew it for negative numbers but I wanted to keep it simple :) – Corralien May 13 '21 at 20:47

0 Answers0