I've always known that Python augmented operations always perform in-place operations. But seems like it's not true for every case. When I apply Augmented operations on integers
, it's not in place.
var1 = 1234
print(id(var1))
var1 = var1 + 4321
print(id(var1))
print()
var2 = 5678
print(id(var2))
var2 += 8765
print(id(var2))
Output:
140272195234704
140272195234928
140272195234736
140272195234896
But when I apply it on lists, it's in place.
var1 = [1, 2]
print(id(var1))
var1 = var1 + [3]
print(id(var1))
print()
var2 = [5, 6]
print(id(var2))
var2 += [7]
print(id(var2))
Output:
140461597772992
140461597413184
140461597412480
140461597412480
My question is, when does it behave as an in-place operation and when does it not? And why?