Making Python code beautiful

A bit of Python code can be written so well that your boss and coworkers will marvel at its beauty. Keep reading to discover easy changes that will improve your code’s speed and style!

Treat this as a collection of useful concepts. Even you Python pros will find tips to apply to your code.

Tuple unpacking

But in Python, this is all shortened to just one readable line:

Thats it! This is faster and more space-effective. Maybe your code is prettier already.

Python pros — did you know that you can use * to assign a list of values to a variable?

Line length

This leaves more whitespace and aligns the operators (e.g. + ), making it easier for somebody (including your future self) to understand the code.

Python pros — You can also look into linters, which automatically format your code for you! Linters go beyond line length: nested lists, spacing, etc. I use the Black linter and I love it.

List comprehension

If you ever catch yourself doing that, change it to this:

It’s faster and it’s a lot cleaner.

Context managers

First of all, file pointers are meant to be closed! Secondly, there’s a beautiful Python syntax that lets you forget the opening and closing:

Python pros — You can actually write your own context managers. These are useful for code that has a setup and an ending action. The general structure looks like this:

Say you wanted to have a context manager that printed to let you know when your processes start and end:

That’s it! Now you can use it like this:

Positional-only arguments

  • Positionally: is_capitalized("world")
  • Not positionally: is_capitalized(string="world")

In Python 3.8, you can force arguments to your functions to be positional! This can make code cleaner and more readable.

To achieve this functionality, use a / as an argument:

All arguments that precede the / will need to be passed positionally:

Going further

If you want to look at tips from others, here are two good places to get started:

Raymond Hettinger’s PyCon 2013 talk about idiomatic Python code. In it, he goes over many little changes you to your programs that make them nicer. The talk was part of the inspiration for this article. Note, the talk shows code in Python 2. The top comment on the video explains the few changes you’ll need to make for Python 3.

Conor Hoekstra’s PyCon 2020 talk about refactoring Python code. In it, he shows how you can shrink a 60-line file to one-third the size. Note, at 14:51 in the video, Conor turns a try-except into an if-else. I discourage this, since it violates Python’s easier-to-ask-permission-than-forgiveness (EAFP) principle. Learn more about EAFP in this article by Brett Cannon from Microsoft.

Thanks for reading! Consider giving me some claps if this article was helpful to you.

Check out my website at thomasbreydo.com.

NYC-based developer, student, and CS enthusiast.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store