Thursday, September 22, 2016

Python is a dynamically typed language.... is this good or bad?

In Python, everything is an object, be it an integer, a string, a function, a module or any other value; each object has a type associated, which is resolved during runtime. Being a dynamically typed language, when a value is assigned to a variable, python creates an object and assigns the resolved type to the object's attribute __class__. Then, it binds the object to the name that is on the left side of the assignment operator. If you have noticed I have changed the usage of the entity that is on the left side of the assignment operator from variable to name. That is because a variable in Python is just a name, generally called an alias, that is bound to an object containing the value that is on the right side of the assignment operator. Therefore, the same name can be used for different objects(types of value). On the other hand, in statically typed languages, a variable that is associated to a given type at compile time can only store values of the given type.

So far so good but there can be problems when the programmer makes typographical errors, while assigning values to variables. For example, a new object is created in the second statement, although the programmer intends to modify the same object.


Unlike statically typed languages, Python would silently allow such bugs to percolate into the code. Should we get the guns out and call for making Python a statically typed language. May be no, rarely occurring unexpected bugs such as these would be identified in unit tests. So, do not wrry, Python is goo ;)

Whats coming in a few posts is about Python being a strongly typed language, which makes it wary and difficult to beguile.

No comments:

Post a Comment