This question already has an answer here:
- How do I pass a variable by reference? 24 answers
Okay a very silly question I'm sure. But how does python assign value to variables?
Say there is a variable a
and is assigned the value a=2
. So python assigns a memory location to the variable and a
now points to the memory location that contains the value 2
. Now, if I assign a variable b=a
the variable b
also points to the same location as variable a
.
Now. If I assign a variable c=2
it still points to the same memory location as a
instead of pointing to a new memory location. So, how does python work? Does it check first check all the previously assigned variables to check if any of them share the same values and then assign it the memory location?
Also, it doesn't work the same way with lists. If I assign a=[2,3]
and then b=[2,3]
and check their memory locations with the id
function, I get two different memory locations.But c=b
gives me the same location. Can someone explain the proper working and reason for this?
edit :-
Basically my question is because I've just started learning about the is
operator and apparently it holds True
only if they are pointing to the same location. So, if a=1000
and b=1000
a is b
is False
but, a="world"
b="world"
it holds true.
I've faced this problem before and understand that it gets confusing. There are two concepts here:
So let's consider the case of a list (you accidentally stumbled on interning and peephole optimizations when you used ints - I'll get to that later)
So let's create two identical lists (remember lists are mutable)
See, despite the fact that the lists are identical,
a
andb
are different memory locations. Now, this is because python computes[1,2]
, assigns it to a memory location, and then calls that locationa
(orb
). It would take quite a long time for python to check every allocated memory location to see if[1,2]
already exists, to assignb
to the same memory location asa
.And that's not to mention that lists are mutable, i.e. you can do the following:
See that? The value that
a
holds has changed, but the memory location has not. Now, what if a bunch of other variable names were assigned to the same memory location?! they would be changed as well, which would be a flaw with the language. In order to fix this, python would have to copy over the entire list into a new memory location, just because I wanted to change the value ofa
This is true even of empty lists:
Now, let's talk about that stuff I said about pointers:
Let's say you want two variables to actually talk about the same memory location. Then, you could assign your second variable to your first:
Look what happened there!
a
andb
are both assigned to the same memory location. Therefore, any changes you make to one, will be reflected on the other.Lastly, let's talk briefly about that peephole stuff I mentioned before. Python tries to save space. So, it loads a few small things into memory when it starts up (small integers, for example). As a result, when you assign a variable to a small integer (like
5
), python doesn't have to compute5
before assigning the value to a memory location, and assigning a variable name to it (unlike it did in the case of your lists). Since it already knows what5
is, and has it stashed away in some memory location, all it does is assign that memory location a variable name. However, for much larger integers, this is no longer the case:Your understanding is generally correct, but it's worth noting that python lists are totally different animals compared to arrays in C or C++. From the documentation:
id(obj) Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
The simple answer to your question is that lists in python are actually references. This results in their memory addresses being different as the address is that of the reference as opposed to the object as one might expect.
This is an optimization that python performs for small integers. In general, you can't count on a and c pointing to the same location. If you try this experiment with progressively larger integers you'll see that it stops working at some point. I'm pretty sure 1000 is large enough but I'm not near a computer; I thought I remembered it being all integers from -128 to 127 are handled this way (or some other "round number").