How To Encourage Best Practices in Python Programming By Complying With PEP8 Style Guide
As part of this blog post, I have added a YouTube demo on how to enable PEP8 compliance in Visual Studio Code.
Enabling PEP8 Compatibility of Python Code in Visual Studio Code - YouTube
Some of the best practices in Python programming are:
Comply with PEP8 conventions.
Enforce data type checking.
Autogenerate docstring
Install autopep8 for code formatting.
Complying With PEP8 Conventions
The key benefit of following best practices and conventions in Python programming is to improve the readability and consistency of code. The readability of code is very important because code is read much more than it is written. The readability of code cannot be over emphasized because it helps to improve the maintainability of code. Following the standards in PEP8 will go a long way in improving the quality of the code we write. PEP8 convention provides a style guide for writing consistent and readable Python code and I will show you how to enforce or encourage PEP8 conventions in Visual Studio Code. The first step is to install PEP8 which has the Python coding standards such as variable naming style, module docstring, function docstring, and inconsistent indentation.
The first step is to Install PEP8 by running the following command:
$ pip install pep8
The next step: To enforce the PEP8 standard, we will install Pylint in Visual Studio Code. Pylint is the tool that checks whether our code complies with the PEP8 standard and returns errors where we fail to comply. Install Pylint The easiest way to install Pylint is to go to the extension tab in Visual Studio Code, search for Pylint and then install it.
Another way to install Pylint is to run the following code on the terminal:
$ pip install pylint
Then Pylint needs to be enabled on VSCode by following these steps:
Press "Ctrl + Shift + P" to get Command Palette
Type "Lint"
Select "Python : Enable/Disable Linting", and click on "Enable"
Repeat Steps 1 & 2, now select "Python : Select Linter", Select pylint from options
Note: apart from highlighting stylistic problems, Pylint highlights syntactical errors in your code.
Enable Type Checking
You might also enable type checking in VScode by going to settings, typing “type checking” and changing type checking mode to either basic or strict. This will highlight function parameters without data type specified.
Autogenerate docstring
For a large project, writing docstring could be cumbersome, so I use autoDocstring to automatically generate docstring for modules and functions. To install autoDocstring, go to an extension on VsCode, type autoDocstring and then install it. To generate a docstring, Cursor must be at the line where you want your docstring to start, right click and then click “Generate Docstring”. You must write your function first before you can generate a docstring for it.
Install autopep8 Formatter Extension for VScode
autopep8 automatically formats Python code to conform to the PEP8 style guide. Install autopep8 formatter from extensions in Vscode.