Weblog of an Aspiring Computer Scientist
Random header image... Refresh for more!

Python Variables

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:

3 Sticky Notes Attached One Above Another

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.

OR

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.

2 comments

1 Deadpan110 { 05.12.08 at 5:44 am }

This seems perfectly logical to me as for instance (within PHP):
$somevar = "abcde";

The length of the string in bytes is a certain length and without using references, the following would happen:
$somevar = "abcde";
$somevar = "fghijklmn";
echo $somevar;
Output:
fghij;

The rest of the string would not fit into the same ‘box’ as the initially set variable.

Things are more interesting when a variable is referenced within different scopes.

Take the following example from PHP: unset - Manual:

function foo()
{
static $bar;
$bar++;
echo "Before unset: $bar, ";
unset($bar);
$bar = 23;
echo "after unset: $bar\n";
}

foo();
foo();
foo();
Output:
Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23

Which leads me to ‘unset’ (there are probably similar functions within all languages)

Does un-setting a variable from all scopes leave empty holes in the programs memory?

The places where the references were initially stored could no longer be accessible - or does the language runtime compiler have a garbage collection and recycle empty space?

Read this comment on occupied memory that is rendered useless as you cannot reference it anymore which asks the same questions.

BTW: nothing wrong with your GIMP skills :P

2 Shriphani { 05.12.08 at 2:36 pm }

Well, here is a justification of my “box” ideology. Let us say that the box is a “one size fits all” variety. Like a large box which has access to loads of memory in the namespace. For instance, in Bill Gates’ 8 bedroom, 25 bathrooms and 4 levels mansion ( I hope I got the number right ), it is just the Gates family that lives. I could live there too and so can that 12 member large family.

Leave a Comment