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”