python decorators

Its very important to remember that in Python, everything is an object.

Functions are objects, classes are also objects. By virtue of being objects, they can be passed around. And they are mutable (which means they can be modified).

Think of a situation where you want to measure the time each function takes to execute. Assume you have 20 functions in your module that you want to measure the execution time for.

Continue reading “python decorators”

python functions and callables

Python is a good, strong Object Oriented Programming language and it was never designed to be a functional programming language. You can read a bit about the history here.

Having said that, Python does have certain useful functional programming features which we discuss below.

Function as first class citizens

All functions in python are in fact objects and you can do whatever you do normally with objects. Like passing them around. Functions can be passed as parameters into other functions and they can also be returned as return values. Continue reading “python functions and callables”

tips on writing beautiful code in python – part 1

I’ve been programming in Python for over 7 years now and I must say its the most beautiful language I have ever worked with. They say “beauty lies in the eyes of the beholder”, but when it comes to Python, the language itself is designed to look beautiful. Ok, maybe I am a bit biased !!

The following tips are heavily influenced by the various talks I watched, by Raymond Hettinger, the co-author of Python (who I admire) and a few of my own experiences with Python. Since this is a list of things I collected over the years, I am not able to attribute these to the appropriate authors, but please accept my sincere thanks.

# tuple unpacking

When you want to do something like swapping the values of two variables a, b

temp = a
a = b
b = temp

a better (and safer) way of doing this is by using tuples to unpack.

(a, b) = (b, a)

Continue reading “tips on writing beautiful code in python – part 1”

playing with IRIS data – KMeans clustering in python

I was revising my statistics and data analytics notes from my dog eared handwritten notebooks and thought it would be a good idea to transfer the notes online. What better place than the blog.

Here is a quick and simple example of the KMeans Clustering algorithm. And to demonstrate the algo, I am using the infamous IRIS dataset. I do apologise if you are bored at looking at this dataset over and over again, but its probably the most simplest and easily understandable dataset for beginners.

Continue reading “playing with IRIS data – KMeans clustering in python”