Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python referencing variables

My this thread raised from previous one

When assigning variables if we reference the variable, then after changing first variable value why in the end b is not changing

a=5
b=a
a=1
print(b)

b prints 5 but for reference logic shouldn’t it be 1?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Assuming immutability of variable a I changed both assignment to lists.

a=[5,2,6]
b=a
a=[1,3,0]
print(b)

But even assigning mutable list, and referencing that object, doe snot differ in result. b again prints a first value assignment [5,2,6]

>Solution :

I may have mislead you with my slightly careless use of vocabulary during that thread. When you assign one variable to another using b = a, b becomes a reference to the same thing that a references. But b does not become a "reference to a" (which is what I suggested); a is already a reference to some object stored in memory, and b now references that same object.

You are correct that a change in a "should" cause a change in b too, because they both reference the same object – the same data stored in memory.

But that doesn’t happen, because the object that you are trying to change is immutable.

Immutable variables

Most basic data types in Python, such as ints and strings, are immutable. Immutable means "unchangeable" – you cannot edit the value of these objects after their creation.

So when you try to edit a variable, which is referencing an immutable object, the value of the object cannot actually be edited.

Instead, here is what happens:

  1. A new object is created in memory
  2. This new object is given the value that you have assigned to the variable. In your example, the new object is given the value 1
  3. The variable you were trying to change is made to reference this new object, instead of the old one. So it appears that the value of the old object has changed.

But actually, the value of the old object has stayed the same. a now references a newly created int with the value 1, but b still references the old object that a used to reference.

So b still equals 5.

Mutable variables

The reason that the problem occurred in your previous question, the one which I originally answered, is because you were dealing with lists, not ints.

Lists are not immutable, they are mutable – they can actually be changed.

so when you edit a variable which is referencing a list, that whole three-step process of creating a new object is no longer necessary. Instead, the original object gets edited.

So all of the references to the object are actually changed, not just the one you were directly trying to edit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading