Four Things to Love About Python

shutterstock_184814297_python

It should have been easy to compile a shortlist of things that make Python a go-to language for so many programmers. But it turns out that the very things that make Python so popular also make it hard to winnow down the list.

Should I write about duck typing, the Python Package Index, the awesome and helpful Python community, or simply reiterate the Zen of Python? In the end, I stuck with the things I feel impact me the most in my daily work, and the things I feel hampered by when I find them missing from other programming languages.

1. The REPL (interactive console)

If you’re not used to programming in a language with a REPL (Read-Eval-Print-Loop, or simply the “interactive console”) you don’t know what you’re missing. In fact, many programmers have been coding Python for a long time without ever using it.

When you do start using the REPL, however, it can change the way you program forever. Instead of trying to make sense of bad documentation or create an entire mini-project to try something out, you just fire up the console and explore. Whenever someone seems confused about why Python’s introspective capabilities is a big deal, I always assume they don’t use the REPL–the ability to inspect and explore code gives you a very powerful tool to understand the inner workings of Python and of the libraries and frameworks you use.

Now, whenever I do any kind of novel programming in a language without a REPL I feel like I’m coding with my hands tied behind my back. Unit tests, IDE support, and documentation helps to a point, but they are no replacement for the freedom of the REPL when exploring a system, language, or tool.

2. The power of white space

The thing that for the longest time kept me from teaching myself Python was the white space. I just could not stand the invisible tab vs. space errors, or the thought that the language forced me to format my code in a specific way. Since then, ironically, I’ve come to cherish Python’s white space. Because Python uses indentation for blocks, ‘creative’ indentation tends to break the code outright, which prevents a lot of headaches by letting you quickly scan code to understand it or find a bug. In languages that do not enforce uniform formatting, large code bases tend to accumulate weird little snippets – for example:

if(condition())
do_something();
do_something_else();

These are just bugs waiting to happen the next time someone edits the code.

In fact, I now use four spaces, never tabs, no matter what language I’m coding in–and my editors complain if I don’t. That has the added benefit that I can always copy and paste my code anywhere and expect it to display properly.

3. The Python standard library: batteries included

I have written a number of advanced tools and frameworks using nothing but the Python standard library (https://bitbucket.org/haard/hgapi, for example) which allows simple Mercurial integration in Python programs, and I feel empowered by the amount of tools that are included. While some of the included libraries are a bit cumbersome, the fact that it is not only possible but feasible to create complex applications with no external dependencies outside of Python can be a huge boon.

You may know that you can start a web server using nothing but this:

$ python3 -m http.server

But you should also know that the http library is powerful enough for you to build custom HTTP and CGI servers using nothing but Python and the standard library. This may or may not be something that is important to you personally, but Python’s ability to let you solve such a wide array of problems without third-party dependencies can be a real help, especially when working on servers where it is impractical or impossible to install extra packages.

4. Comprehensions

Many Python programmers go years without learning list and dict comprehensions. To be sure, there are other ways of solving the issues the cases comprehensions address, but taking the opportunity to really understand how they work can make your code faster and let you add a declarative aspect that makes it easier to read, once you learn the knack of reading them.

Consider this snippet:

>>> my_list = []
>>> for i in range(10):
...     my_list.append(i*i)
...
...
>>> my_list
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

It works, but even this simple example needs to be read in full to see what it does. Compare that to a list comprehension that does the same thing:


>>> my_list = [i*i for i in range(10)]
>>> my_list
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

With the comprehension, the code is not only faster, it’s terse enough that you often don’t need to introduce a variable name at all, which keeps your namespaces cleaner and your code a lot more readable–at least to people who can read comprehensions–than the equivalent code would be without comprehensions.

These four things represent a small sample of the many little things about Python that together make it the language I keep returning to, even though I try to learn and use other languages at least once or twice a year. For example, while dynamic type systems and powerful introspection capabilities are not things every developer uses, they could each be (and have been) the subject of entire articles of their own. Just as important to me, the amazing and heterogeneous Python community may have nothing to do with the technical merits of the language, but interacting with other Python programmers contributes significantly to my enjoyment and productivity.

*Python snake image courtesy of Shutterstock.com.

Fredrik Håård is a freelance developer at Metallapan AB, as well as co-founder and head developer at Visual Units, a Swedish company creating support tools for logistics. In addition to working as a developer, designer, and general troubleshooter, Fredrik holds courses and workshops in Python, Mercurial, and Git and has given lectures in Python at Blekinge Institute of Technology. Lately he's been talking at way too many conferences, mainly about Python, which predictably resulted with his becoming chair of the first-ever PyCon Sweden in 2014. View posts by .

Interested in writing for New Relic Blog? Send us a pitch!