DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for [Python] Beautify your code using Auto-Formatter
Kelvin
Kelvin

Posted on

[Python] Beautify your code using Auto-Formatter

What is Code Formatting?

Code formatting is effort to makes code easier to read by human by applying specific rules and conventions for line spacing, indents, spacing around operators, and so on see more details here. Formatting shouldn't affect the functionality of the code itself.

Linting vs Formatting?

On the other hand, linting is made to analyzes code for common syntactical, stylistic, and functional errors as well as unconventional programming practices that can lead to errors. Although there is a little overlap between formatting and linting, the two capabilities are complementary.)

Auto Formatter

An auto formatter is a tool to automatically format your code in a way it complies to the tool or any other standard it set. Normally there's 2 option when we want to integrate this tool on our workflow. First is through your favourite text-editor and second is using pre-commit hook. In this article i will explain more about the first option which integrate auto formatter in our text-editor for better code standard. Advantage of using auto formatter are :

  • You don't worry about low-level problems since it's fixed automatically
  • Reduces number of discussions or debate about code style so developer can focus writing actual code
  • Help new developers more familiar with the code base because the style is consistent
  • Less merge conflicts

If you have experience writing Golang and Javascript there's chance you're already using auto formatters like gofmt for Golang or prettier for JavaScript. Now the question, how we can do the same thing in Python?

In Python there are three auto-formatter available:

autopep8

autopep8 is an open source auto formatter built and made by several developers. It uses pycodestyle to analyze which parts of your code do not fit to the pep-guidelines and will try to fix them. As of the time of writing the project had around 3.3K stars (April 2020).

yapf

Yet another Python formatter is tool produced and maintained by Google. It has ~10.5K stars (April 2020) on GitHub. Yapf follow different approach compare with other auto formatter tools.

In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide. Style remains consistent throughout the project and there's no point arguing about style in every code review.

black

The most popular auto formatter in Python. It has around ~15K stars on GitHub (April 2020). Black is loved because you don't need to configure anything in order to format your code. Javascript Prettier also follow this approach.

Get Started

In order to get started you just need to install one of package above.

autopep8

pip install autopep8
autopep8 --in-place --aggressive --aggressive <filename>
Enter fullscreen mode Exit fullscreen mode

yapf

pip install yapf
yapf <filename>
Enter fullscreen mode Exit fullscreen mode

black

pip install black
black <filename>
Enter fullscreen mode Exit fullscreen mode

What is your favourite Python Auto-Formatter?. Let me know by adding comment below :)

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›