PEP8 style guide for Python
PEP8 is a style guide for Python that helps programmers write readable code. It is important to follow certain conventions to make our code readable. Some examples of coding conventions include the following:
- Inline comments should start with a
#Â
and be followed by a single space. - Variables should have the following convention:Â
first_var
. - Avoiding trailing whitespaces on each line. For example,
if name == "test":
should not be followed by whitespaces.
Note
You can read the entire PEP8 standards at https://www.python.org/dev/peps/pep-0008/#block-comments.
Verifying PEP8 guidelines
There are tools to verify PEP8 standards of your code. After writing a code sample, ensure that your code adheres to PEP8 standards. This can be done using the pep8
package. It can be installed as follows:
pip3 install pep8
Let's check whether one of our code samples has been written according to the PEP8 convention. This can be done as follows:
pep8 opencv_test.py
The check indicated the following...