How Learning Python Changed My Life

Part I

hamid
4 min readJul 13, 2014

This is not yet another rant about some developer’s affinity to Python. It is rather an accurate account of the impact that learning Python had on my skills and capabilities.

I got to learn Python out of genuine curiosity shortly after starting my first job at one of Microsoft’s first R&D labs in Egypt (late 2006). Back in the day, I could only write C++ and C#. I used to write lots of C# snippets to automate several data massaging and text processing tasks.

My initial impression was very positive. I liked the clean syntax and was intrigued by the boldness and creativity behind using indentation to define blocks of code. On the syntactic level, I liked keyword arguments, list comprehension and all the various ways you could index into sequences. I was especially fascinated by all the standard libraries Python ships with (which Python enthusiasts like to refer to as “batteries included”).

Python felt a lot more convenient than C# for general scripting and text processing tasks. And although PythonWin did not come across as the most impressive or capable IDE, it was very usable and lightweight. It felt a little broken but in a fairly useful way. It also featured a very easy-to-use debugger and was perfectly adequate for everyday scripting in general.

Functional programming

I was keen on learning more about Python, so I started looking around for books and materials. It wasn't long before I came across David Mertz’s excellent “Text Processing in Python” among a couple of other books. Text processing was definitely something I did a lot, so I was wondering if there was anything out there to help.

One of the earliest parts of the book tackled the issue of utilizing “Higher-Order functions” in text processing. I did not really know what Higher-Order functions were at the time nor was I acquainted with Functional Programming in the first place. I followed the book closely and got mixed feelings about the concept in the end. On the one hand, I was quite impressed by the terseness and succinctness of the code presented in the book. For instance, I really liked being able to compose new functions by combining existing ones. Here’s an example:

>>> def add(a, b):
... return a + b
...
>>> def double(a):
... return 2 * a
...
>>> add_then_double = compose(double, add) # add_then_double(x, y) = double(add(x, y))
>>> add_then_double(10, 20)
60

We simply took two functions, add() and double(), and composed a new function, add_then_double(), out of them, one that simply does double(add(a, b)) without having to make any changes to either function.

On the other hand, I was somewhat puzzled when I tried to understand how compose() actually worked. But the usefulness of the concept was so evident I could not ignore it. I mean, think of all the possibilities it enables:

  • Composing a function that does authentication with another to make sure the latter is only available to authenticated users. (Aspect-oriented programming fans will relate. Boy, wait until they learn about Python decorators.)
  • Composing a function that takes long to do something (e.g. one that downloads a webpage) with another that does caching (e.g. to get a new function that would not crawl the same page twice).
  • Composing a function with another to add a timeout capability.
  • Composing a function with another to make the code run in a different thread.

The possibilities are endless! There are many creative ideas the Python community has developed using Higher Order functions. In fact, almost all of the above-mentioned ones are already implemented in Python Decorator Library.

Although David Mertz’s book was published more than 10 years ago, I think it is still an incredible source of knowledge about Python as well as advanced text processing techniques that are not specific to any language. The book is available free of charge in a very readable text format on the author’s website.

The introduction to Higher-Order functions comes in the very first chapter titled “Python basics”. Appendix A also gives a fairly preliminary and pragmatic introduction to Functional Programming. To be able to understand some of the details in that chapter, you may find it useful to take a look at a brief explanation of Python’s built-in map(), filter(), reduce(), zip(), and lambda, most of which are explained in very simple terms right here.

Functional Programming is a very interesting paradigm, and the first chapter of that book demonstrates some of its useful applications. This is not to say that Python is essentially a functional — or even functional-first — programming language, although it does definitely support the paradigm to some extent. However, playing around with Python in the ways illustrated in the examples of the first chapter of that book showcases some very handy uses of the paradigm and enables us to play with them in a an easy and approachable manner.

This was one of the most profound effects Python had on my technical skills. It was such a nice introduction that paved the way for later endeavors with Lisp and Haskell. But that’s a story for another day.

[Liked it? Read on: Part II: Extensibility and libraries]

--

--