Alec
Alec

Reputation: 9546

Variable assignment and modification with regards to memory addresses

Suppose I assign two variables to integers:

a = 1
b = 2

Now, I'll assign a to b:

a = b

As expected, a == 2, because a has been set to the memory address of b.

But actually, it hasn't. If I do

b += 1

a still equals 2. Why does a not point to b?

Upvotes: 4

Views: 2110

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

The behavior in the example is as follows

In [1]: a = 1                                                                                                                                                                     

In [2]: b = 2                                                                                                                                                                     

In [3]: a = b                                                                                                                                                                     

In [4]: b+=1                                                                                                                                                                      

In [5]: b                                                                                                                                                                         
Out[5]: 3

In [6]: a                                                                                                                                                                         
Out[6]: 2

In the example, when you do a=b, both a and b are pointing to the same reference, but when you b += 1, the operation of adding 1 to b, creates a new integer value 3 for b and b points to that value, but a is still pointing to the old value 2

Note that trying to do it with mutable types like a list works as what you were excepting would happen to an integer

In [1]: a = [1]                                                                                                                                                                   

In [2]: b = [2]                                                                                                                                                                   

In [3]: a = b                                                                                                                                                                     

In [4]: b.append(2)                                                                                                                                                               

In [5]: a                                                                                                                                                                         
Out[5]: [2, 2]

In [6]: b                                                                                                                                                                         
Out[6]: [2, 2]

In [7]: b += [3, 4];                                                                                                                                                                        

In [8]: b
Out[8]: [2, 2, 3, 4]

In [9]: a
Out[9]: [2, 2, 3, 4]

Now what happened here? We changed b but a changed as well, this is because the append or update of the list happens in-place and since a was pointing to b both ends up getting updated!

What happens for += operator is defined by __iadd__ method of a class. For int-s all the __iXXX__ methods return a new instance of int. For list-s __iadd__(self, other) does self.extend(other); return self, so variable keeps pointing to same object.

As a result, even integers can be made to behave as a list, an example is here courtesy @imposeren

Upvotes: 4

Toothpick Anemone
Toothpick Anemone

Reputation: 4644

In other programming languages, what you are talking about are called "pointers."
They have this name because they "point" to things.
a points to b, b points to 2, and so on.

I have drawn some pictures to describe what you thought would happen, and what actually happened.

What You Expected Would Happen:

enter image description here

What Actually Happened:

picture of actual behavior

Upvotes: 2

hem
hem

Reputation: 1032

The variables in python are references. Few references are to immutable objects (such as strings, integers etc) whereas lists and sets are mutable.

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# Consider two variables x & y  with integers 23 and 46. They have immutable references
# Meaning, if y=x, it doesn't mean y will change whenever x is updated.
>>> x=23
>>> print (x)
23
>>> y=46
>>> print (x)
23
>>> print (y)
46
>>> y=x
>>> print (x)
23
>>> print (y)
23
# Let's change the value of x after the assignment
>>> x=99
>>> print (x)
99
# Since it is immutable, the value wont change.
>>> print (y)
23
#Let's consider the mutable reference scenario. a & b are two lists which have mutable references
# Meaning, if b=a, it means b will change whenever a is changed
>>> a = list([11,22,33,87])
>>> print (a)
[11, 22, 33, 87]
>>> b = list([87,53,98,2])
>>> print (b)
[87, 53, 98, 2]
>>> a=b
>>> print (a)
[87, 53, 98, 2]
>>> print (b)
[87, 53, 98, 2]
# Popping the list would alter b
>>> b.pop()
2
>>> print (b)
[87, 53, 98]
# Notice the change in value of a
>>> print (a)
[87, 53, 98]
>>>

Upvotes: 0

Related Questions