Saturday, September 24, 2016

Python Meta-programming 1: Set the stage by understanding the execution model

Meta programming is not for the faint-hearted. Just kidding... Well, to put simply, it gives you enormous power and flexibility to get things done, which would otherwise be difficult to accomplish, if not impossible, without knowing under the hood features of the language. For easier comprehension, I will break things down and present to you a series of Python Meta-programming posts.

To begin with, this post deliberates the execution model of Python, which is a stepping stone to understand meta programming.

Being an interpreted language, compilation in Python occurs at runtime. Unlike languages like Java, here in Python, the bytecode is created on the fly and stored in the memory itself as code objects of units, generally referred to as code blocks, viz. module, function, and class. For each block, a namespace is created using a dictionary and stored as an attribute of the object called __globals__.

It is important to note that variables in python, similar to Java's aliases, are just names that are bound to objects. Python binds a name to an object when it executes constructs like import statements, function and class definitions, and assignment operations.

Scoping of the names bound in the code blocks occurs at two levels, global and local. The module block defines the global scope or namespace. Whereas, names defined in other blocks are local to the block and any block enclosed within it, except when the enclosed block rebinds the name to another object. A local name can be elevated to global namespace by qualifying it with the global statement.

Knowing this we can now start walking down the meta programming lane, like a monk walking blissfully in the thick forests, and totally not being unaware, pun intended.

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.

Monday, September 19, 2016

Data Analysis: Frequency Polygons vs Boxes to compare histograms of different datasets

Histograms are created using boxes representing bins, and no doubt they seem to have been aptly selected for depicting the quantity populated into a bin. However, the histogram becomes cluttered if we want to compare two or more datasets.

A better way to represent bin's height would be to place a polygon at the specified height(frequency of the bin), one for each dataset. Hence, bin values of different datasets would render being visually distinct.

Sunday, September 18, 2016

Welcome to my blog

Welcome,

I have created this blog to put forth my views of computing, mathematics, and science. May be it comes with a bias, but I will try to make things as easy as possible, and let brevity lead itself.

I will try and touch many aspects of programming, machine learning, artificial intelligence, parallel systems, and hopefully many other topics but not limited to mathematics, systems biology, genetics, and chemistry.