Python Logo on a stylized background

Python Style Guides

In my pursuit to update my open-source Python Twitter bot, the retweet-bot, I decided to implement style recommendations followed by other open-source projects. This led me to use Pylint and follow the official PEP guidelines in the project. However, I was not prepared for the level of difficulty and pain that followed while implementing style guidelines. While my codebase now follows a consistent style found throughout the open-source Python world, it presented numerous pain points. Through this article, I aim to provide an overview of the pros and cons of Python style guidelines, and to enlighten readers to view these guidelines as flexible, especially for older codebases.

As developers, we have a responsibility to ensure that our code is of high quality and meets certain standards. This is the primary focus of Python style guidelines, and we will explore the pros and cons of both Pylint and PEP8 in this article. I will share my perspective as a relatively novice Python developer.

Pylint

Pylint is a static code analysis tool for Python that checks for coding errors, redundancies, and other issues. It is widely used by developers to ensure that their code adheres to certain standards and is of high quality. Here are the pros and cons of using pylint:

Pros:

  1. Pylint helps catch coding errors early on in the development process. This saves time and effort in debugging later on.
  2. Pylint can check for coding conventions and standards set by various organizations such as PEP8. This ensures that the code is consistent and easy to read for other developers.
  3. Pylint can help improve the maintainability of the code. By catching potential issues, it helps to keep the codebase clean and understandable.
  4. Pylint is highly configurable and customizable. It can be set up to check for specific coding rules and ignore others that may not be relevant to the project.

Cons:

  1. Pylint can be overwhelming for beginners. With its numerous rules and options, it can be difficult to know where to start.
  2. Pylint can be too strict at times. It may flag certain code as an error even if it is perfectly functional.
  3. Pylint can be too lenient at times. It may not catch certain errors that are not explicitly stated in its rules.

Python 3 Style Rules (PEP8)

Python 3 style rules are a set of guidelines for writing Python code. They are not enforced by a tool like pylint but are instead recommended by the Python community. Here are the pros and cons of using Python 3 style rules:

Pros:

  1. Python 3 style rules are easy to understand and follow. They provide a consistent style for Python code, making it easy for developers to read and understand each other’s code.
  2. Python 3 style rules are flexible. They can be adapted to fit the needs of the project or the team.
  3. Python 3 style rules are widely accepted by the Python community. This makes it easy to find resources and examples that follow these rules.

Cons:

  1. Python 3 style rules are not enforced by a tool like pylint. This means that it is up to the developer to ensure that they follow the rules.
  2. Python 3 style rules can be subjective. What one developer considers “Pythonic” may not be the same for another developer.
  3. Python 3 style rules can be too general. They may not cover all aspects of coding, leaving room for errors and inconsistencies.

Conclusion

Pylint and Python 3 style rules both have their pros and cons. Pylint is a powerful tool that can catch potential errors early on and enforce coding standards. However, it can be overwhelming for beginners and too strict at times. Python 3 style rules, on the other hand, provide a consistent style for Python code and are easy to follow. However, they are not enforced by a tool and can be subjective or too general. Ultimately, it is up to the developer to decide which approach to take based on the needs of the project. My personal recommendation is to take these style recommendations with a pinch of salt, aiming for perfection is never a bad idea but don’t let it be the enemy of good.


Also published on Medium.