Python PEP8

Python PEP8
Techiio-author
Written by Sagar RabidasDecember 22, 2021
10 min read
Python
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Python PEP8.

Python PEP8

Pep or python enhancement inspiration is a draft or file that has the outline of python code writing recommendations, which is a pleasant exercise to enhance the python codes’ consistency and readability. This file carries capabilities together with python’s style and layout that is used even as writing the codes. As we realize, python has a strict layout or the order to write the scripts, so that it makes it others smooth to examine the code; therefore first-class coding fashion facilitates enormously to the builders or others who are studying the code. The developers must comply with these suggestions. In python, we see indentation is very crucial 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:

  • Indentation

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 is 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.

Example

Code:

n = 10
if n> 5:
print “n is greater”

  • 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 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.

Example

Code:

class ClassName: #Class naming rule
C = 1 #Constat naming rule
the_variable = 2020; # variable naming rule
print("The constant value is:", C)
def the_method(self): # method naming rule
print("I'm inside class ")
def insideclass(self):
print("The Variable: ",ClassName.the_variable)
self.the_method()
n = ClassName()
n.insideclass()
  • 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 method. 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.

Example

Code:

def addition:
a, b = 0
“““ This method is for addition”
c = a + b
“““ This method is for addition and it is the 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.

Conclusion

Pep8 is one of the tools for correctly writing python codes with proper rules and styling for the codes. This documentation of guidelines could be very crucial for the developers to write code that is extra readable and much less complex for others. To word one factor, typically writing proper codes with proper comments and files facilitates as codes are written best as soon as, however, many humans study them many times, so the developers want to write the code to be readable and smooth to understand code for others. Therefore pep8 could help you do this.

Python
Python PEP8
linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance