An Ode to Python
I am a Python fanboy. In my time, I've known many programming languages of different paradigms, hailing from different families. I stumbled upon Python about 10 years ago. As a newbie, I was struck by its simplicity and welcoming nature. Python made me feel at home and to this day, I continue to be enamoured by its irrepressible charm which is hard to easily describe and the subject of this long essay.
I think there's a Dutch saying that goes somewhat like “honesty in small things is not a small thing”. I often think about that quote and admire the cultures and people who live by it. More importantly, I associate that quote with Guido Van Rossum - Python’s "Benevolent Dictator for Life"- BDFL - one of my all time heroes.
Here's my ode to Python’s many “small things”. Each of these append to that irrepressible charm that had me at "hello world".
Zen of Python
If the whys are clear, the whats and hows follow. In Python world, we abide by Tim Peters' moral law. The Zen illuminates the Python way. Each zen is simple yet so profound. Each capturing an idea to strive for deep inside our software. Some even encapsulating an ideal to aspire for deep within ourselves. Its hard to be the same human being after you absorb these..
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>
Standard Library
Have you ever experienced betrayal? Like when your 7 friends and you get your hands on a shiny new football, and you can't find an air pump to inflate it? Or when the new remote you've ordered has arrived only to find that AAA batteries were not shipped with it ? Python understands your pain and has got you covered. Python makes sure that it supplies you with all the batteries you need to accomplish a wide range of programming tasks. These are its built in modules.
Between csv, Queue, subprocess, sys, logging, threading, multiprocessing, urllib2, asyncio and a ton of others, most programming obstacles can be overcome by merely chanting a mantra or two from Python’s exhaustive Standard Library. Invest in praying to these Gods. There are no market risks here. Dividends are a certainty.
List and other (generator, dictionary) comprehensions
I am not originally from a Functional Programming background. Er. strike that. I am not a trained Computer Engineer. The first time I saw Python’s list and generator comprehensions, I was outraged by the idea it represented.
List comprehensions are not about eliminating a few lines from the body of a for loop. List comprehension is a new invention in notation. A notation to help us manipulate iterables. A notation that concisely transforms and filters complex collections through simple expressions. That takes less than a line. That last sentence is important!
Any new way to annotate concepts and express them under 1 line, fundamentally pushes a field forward. Learning list comprehensions changed me as a programmer. Profoundly too. Manipulating and transforming iterables accounts for over 70% of all programming I've ever done.
Context Manager
Today I classify myself as a defensive, paranoid and unsure programmer. I remember starting out as a programmer having the exact opposite traits. Years of debugging core dumps, memory out of bounds corruption, buffer overflows and resource leaks in a predominantly C++ code base have permanently scarred me. I've always wished for languages or tools that make it impossible to commit code having resource leaks. Garbage collection is 1 huge step in that direction. But there are still resource leaks. Resources like open file descriptors, locks, database connections from connection pools are notoriously accident prone. Unless you have stringent coding guidelines or strict reviews, you are prone to encounter unpleasant situations out there in the wild. Context manager through "with statements" in Python is an elegant solution addressing resource problems. They are like seat belts on a fast Expressway. I use them all the time. Even in throwaway scripts and weekend hacks. They have become part of my muscle memory.
Bye bye spending nights tracking down memory leaks !!
Hello solving business problems !!
The Read-Eval-Print-Loop - REPL
One of the benefits of an interpreted language is the privilege of working in a REPL. REPL is the ">>>" prompt that comforts all devotees when they type "python" at their command lines.
All creative work requires interactivity and feedback during the process of creation. A big part of interactivity in programming is the ease and immediacy with which you can see the effects of code as you are typing it. Statically typed languages suffer from a longish edit, compile, run cycle that comes in the way of programming Flow. Firing up the REPL to validate an idea that just struck you in the shower or fine tune an algorithm is an invaluable ingredient that catalyses the elusive Flow. To the REPL I am ever grateful. Also like local anaesthesia in a root canal, the REPL has helped me numb down the pains of many a never ending Mumbai commute.
Decorators
A natural consequence of functions being first class citizens. Ideal way to implement cross cutting concerns ranging from security, logging, instrumentation and profiling. I crave for the productivity and the endorphin rush, an intense bout of Meta Programming often triggers. Decorators are an elegant alternative to craft Aspect Oriented solutions using meta programming.
The Community
The Python community has an enviable critical mass of enthusiastic contributors and vibrant speakers. Just look at the incredible ground a typical PyCon covers! A lot of my personal growth has come about by listening to Guido, Dave Beazley, Peter Norvig and many others who selflessly distil and disseminate their otherworldly wisdom. Also the fact that Python is pervasive in cutting edge scientific and medical research, helps in defining the attitudes and raising the intellectual weight of the community.
Conciseness
Programmers and Programming languages should not be self-infatuated. The language should not come in the way of expressing ideas and abstracting entities that solve problems. The language should not impose its philosophies on the solution. The language should not be too verbose and make the programmer perform unnecessary rituals to perform simple tasks. All languages I have used so far suffer from some of the above problems.
Over the years I have come to regard Python, as a concise, symbolic medium to reason about software. In the domain of software, the medium of communication should be far more powerful, formal, concise and unambiguous than spoken languages such as English. I strongly believe that just as they have “legalese”, that long worded, sleep inducing language lawyers use to write up contracts, Python should be the lingua franca between Engineers to describe specifications, algorithms and what really goes on in systems involving computers. Look around you. So much of our lives is governed by software. Most of it is poorly understood because spoken language cannot begin to describe its inner workings, its inner spirit. This is tragic. But maybe there are opportunities to dive in and decipher this ever expanding world, describe it better for everyone's benefit and then make it function better.
Frameworks
Investing your time in learning programming languages is no different than investing your money across various asset classes, say Real Estate. In Real Estate there are 3 important things to bear in mind. Location. Location. Location. Similarly, investing in learning Programming languages there are 3 things that one should look out for. Ecosystem, Ecosystem, Ecosystem.
Using frameworks that spring from the Python ecosystem, you can get jump started on a wide range of projects. These range from Web Development to Network Programming, Dev Ops, Data Science and AI. This is further testimony to the bustling community around Python. Frameworks like Cython, Pandas,Twisted, Django, Theano are like time machines to a Python developer on a deadline. Furthermore, the frameworks themselves are temples to learn about practical programming and fast track programmer maturity around Domain Engineering, API Design, Code Organisation, Tests and Architecture. Maturity therefore is not a linear outcome only measurable by years of experience.
So does Python have flaws? - Sure it has many and many times to my exasperation! But it gets the important parts right. It makes sensible trade offs. So go ahead and use Python as a benchmark to evaluate other languages and tools. When working in Python, I always feel a powerful hidden presence, nudging me to think a certain way, encouraging me to be clearer, inspiring me to be more concise and steering me to be more direct. In my mind, these attitudes remain with you longer than any tool you may eventually employ to solve problems. And thats a side effect I'll happily take any day.
Along with English and Maths, learning and using Python has been one of life’s enriching experiences. Thank you Guido! Take a bow Python! In me you've won an eternal fan.