For a very long time, I was under the impression that variables in Python were like boxes and I could throw objects in them. So, according to me, if I wrote a piece of code that went:
>>> a = "Shriphani Palakodety" >>> print a Shriphani Palakodety
I used to think that there is a new box and I've thrown the string "Shriphani Palakodety" in there. If I do:
>>> a = "Shriphani Palakodety" >>> a = "Doofus" >>> print a Doofus
the general idea is that "Doofus" replaces "Shriphani Palakodety" in box "a".
I was reading a mail on Edu-Sig (Python) and John Zelle ( he wrote an introductory computer science book ) mentioned that variables were not boxes but merely labels ( or tags, or "Sticky Notes" whatever you want to call them ). So, if I do something like:
>>>a = "Shriphani Palakodety"
All I am doing is sticking "Sticky Note" ""a"" on an object ( in this case, "Shriphani Palakodety" ). When I do something like:
>>>a = "Shriphani Palakodety" >>>a = "Doofus"
all I have done is move the Sticky Note "a" to "Doofus" and not remove "Shriphani Palakodety" from box "a" and put "Doofus" in it. Here is my idea of a verification:
>>>a = "Shriphani Palakodety" >>>print id(a) 10353360 >>>a = "Doofus" >>>print id(a) 10386016
So, if a was a box, its location shouldn't have changed. But we observe that a's location changes.
But the Sticky Note analogy can't be entirely applied to variables. In real life, I can stick one sticky note over another and I can have three sticky notes such that one is stuck to the object and the other two are stuck to the sticky note itself. The figure below should illustrate that:
The above picture should depict the following Python code:
>>> x = y = z = 0 >>> x 0 >>> y 0 >>> z 0
So far so good. But let us say, I want to remove the Sticky-Note "y". In real life that move would be represented by:
This means that "z" would be affected too which is not how it works in case of Python. Hence we come to the conclusion that a sticky note should be stuck only on the object and not on another sticky note.
Python's variables ( Sticky Notes ) come with a special kind of glue that isn't sticky on material from which the sticky notes are made. So "x", "y" and "z" should be stuck to the object but not to one another.
This completes what I learned from the Edu-Sig mailing list so far and I hope I joined this list earlier. John Zelle ( who wrote the textbook that MIT now uses for its introductory CS ) is there and I hope to keep learning more.
Well, I would love comments that will improve my knowledge but a comment on my bad GIMP skills will earn you spam and of course, I will call you names as well.