Updated April 14, 2023
Introduction to Python PEP8
PEP or Python Enhancement Proposal is a draft or document that has the description of Python code writing guidelines, which is the best practice to improve the Python codes’ consistency and readability. This document contains features such as Python’s Style and design which is used while writing the codes. As we know, Python has a strict format or the order to write the scripts, so that it makes it others easy to read the code; therefore nice coding style helps tremendously to the developers or others who are reading the code. The developers must follow these guidelines. In Python, we see indentation is very important for code to execute syntactically.
Functions of PEP8 in Python
In general, Pep8 is a tool where you can check your Python code conventions with the conventions in the documentation of Pep8. Let us see a few features of Pep8 documentation:
This is one of the most important features for writing the codes and for reading the codes in Python. It is also known as the 4 space rule, and this rule is not as mandatory as it can be overruled for the continuation of the line. Indentation also helps to know which code belongs to which function as we use braces in other programming languages in Python; this done by following the rules of indentation.
In Pep8, the rules are use spaces in place of tabs, as the name of the rule use 4 consecutive spaces for indentation. If both these rules are used at once then this causes an error that issues a warning by the interpreter.
n = 10 if n> 5: print "n is greater"
The print statement follows indentation in the above program because the “if” statement is true, then only the print statement is executed. If there is no proper indentation maintained, then it will pop an error. The output of the above code without indentation will be:
n = 10 if n> 5: print "n is greater"
2. Naming Conventions
There are few naming rules in Pep8 for Python coding to make the codes more readable and less complex. There are many things in the code to be given a name, such as it may variables, class, methods, packages, etc. It has always been a best practice for selecting names to variables or functions or classes or packages that make sense, or they relate to what exactly the code does because using some random names for declaring would lead to ambiguity or it is highly difficult when debugging the code. Let us see a few naming styles to be used while writing codes.
For variables, you can have either one letter or word or any number of words separated by an underscore, but all these letters should be in lowercase. We can use all the letters in lowercase for naming functions or methods, which can be one word or any number of words separated by an underscore. For constants also follow the same as variables, but all the letters should be in uppercase. For class, the naming rule is that you can use one word or multiple words, but there is no separation between these multiple words, and it follows a camel case like ClassName. For packages also follow the same as naming rules of class, but instead of camel case, the package name’s letters should all be in lowercase. These all can be demonstrated in the below code.
class ClassName: C = 1 the_variable = 2020; print("The constant value is:", C) def the_method(self): print("I'm inside class ") def insideclass(self): print("The Variable: ",ClassName.the_variable) self.the_method() n = ClassName() n.insideclass()
3. Document String
This is also known as docstrings which have the document strings enclosed within both single and double quotes, which are used to define the program or any particular function or methods. The rules for applying document strings to code are:
Firstly the quotation used for documenting a block of code is done in triple quotes such as.
“This is a docstring””” and secondly where it can be used in writing docstring for all functions, public modules, classes, and methods. Note that docstrings are not necessary for non-public methods; instead, you can have comments to describe the description of what the method does. Also, note that the end triple quotes come in the same line for one line docstrings, but for multiple lines, the end triple quotes come where the docstrings end.
def addition: a, b = 0 """ This method is for addition""" c = a + b """ This method is for addition and it is addition of two numbers. This has a formula as shown above c = a+ b And the addition of two numbers gives the result which is stored in c""" return c
Some of the other few Pep8 documentation rules for Python codes are:
- We have to use UTF-8 or ASCII encoding for Python coding, and it is also a default encoding that is meant for international environments.
- There is also a rule of where to use spaces. Spaces should be used only around the operators and after the comma, not inside the brackets or before the comma.
- Characters should not be used for identifiers as they may lead to confusion, such as for letter “l”, which can be taken as lowercase “l” (el) and uppercase “I” and for the letter “O” capital O (oh) and number zero “0”.
There are many different document features of Pep8 for Python code styling and designing.
Pep8 is one of the tools for accurately writing Python codes with proper rules and styling for the codes. This documentation of rules is very important for the developers to write code that is more readable and less complex for others. To note one point, usually writing proper codes with proper comments and documents helps as codes are written only once, but many people read them many times, so the developers need to write the code to be readable and easy to understand code for others. Therefore Pep8 would help you do this.
We hope that this EDUCBA information on “Python PEP8” was beneficial to you. You can view EDUCBA’s recommended articles for more information.