Py02: First Steps towards Python

Py02: First Steps towards Python


As it seems to me, in Perl you have to be an expert to correctly make a nested data structure like, say, a list of hashes of instances. In Python, you have to be an idiot not to be able to do it, because you just write it down.

Peter Norvig


 >>> a_variable = 1

What is that thing I just read? That's a variable. For doing anything in any programming language you are gonna need a variable at some point.

Why it is named a_variable? It is just a choice. You could name it anything from nebula to iron_man. But just think about that, can you perform something to iron man (except you might wanna borrow his suit or Jarvis). Remember always give logical name your variables like computer_science_marks or number_of_cars. Avoid using single letter named variable.

But, what's a variable? Variable in Python is something which is gonna point (not hold) to the value for us.

Wait, why not hold the but point to it? What's the difference? In Python, when a variable is created, it is not same as how C++ does. But when we create a variable, it points to the value/object. If you don't know what an object is, don't worry, right now just think it as a container for our value.

So, if multiple objects hold the same value, it means that value is created only one time and each variable is referring/pointing to the same value. Okay, that just passed through my head...
Don't worry if you didn't understand this concept, I'll gonna prove it you, so fire up your cmd and open your python shell and type the commands from image shown below:

What is happening over here?

Let's break down the command used above and start from the beginning. 

>>> a = 1
>>> b = 2

Here we've declared two variable naming 'a' and 'b' both assigned with value and 2. Calm down, I know the naming of the variable is not as I've suggested but it just for simplifying the example for now.

Then we've used id function of Python. It prints the id assigned to a variable until its lifetime gets over and it is always unique for a variable. And since the value of a and b is not same, therefore there id's are also not same. You can see it in command id(a) == id(b),  which returned false. Here ==  is equality test operator, which returns True or False based on the condition.

Now, there is another variable c which holds the value of a. Since I've said hold, it means it points to the same value as a. It is proved by the command id(a) == id(c), which returned False.

Okay, ids of a and c are same because we've equated the value of a and c. But, why does id(a) == id(d) returned True. Both hold same value but d was never assigned the value of a but was assigned the value 1 which happens to be the same value as of a. Which in turn means that d point to the same object as a. Python doesn't create a new object every time an assignment takes place but only if it doesn't have those object already in the memory.

But C/C++ programmers, wait, always remember what is id returns is not a pointer. id and pointer are completely different from each other. id() function returns an integer, which can be thought of as a social security number of the object.

Wait, it is still not over. Try the above example yourself but with an integer greater than 256, and observe the result. It will not be as expected. id for same integer variables will be different. Why? Let's refer to the official documentation of the language:

GitHub LinkThe current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)


So, it seems like nobody knows why this happens.

Well, that is a lot of information to consume, take a while and let it sink in for today.

Let's sum it up,

  • New variables are created by using assignment operator (=), variables with same value may or may not have same ids. 
  • id in Python and pointers in C/C++ are not the same thing. 
  • The new value is only created only when Python doesn't find reference of the object or if the situation is as in above box.

In next post, we'll talk about datatypes in Python and will play more with Python variable/objects. Objects!!! you read it right, hang on till some more posts, we'll be discussing more on this later.

Try It Yourself:
Create some object and try printing them and their ids with our favorite print function.


Happy Pythoning!!

Comments