Zenesys – Python Coding Standards And Best Practices For Code Quality | Zenesys Blogs

features of pep 8 for better syntax

I will be explaining the crux of PEP8 here. If you want, you can go to visit the official site to read and understand each, and every aspect in detail, click here.
 

1. THE CODE LAYOUT:

This consists of Indentation, the maximum line length to be used, line breaks and blank lines, imports in python, and dunder names. Let’s discuss them with examples.
 

A) IMPORTS, BLANK LINES, AND THE INDENTATIONS:

The import should be in a particular sequence. At first, the standard libraries, then the third party, and at the last, the local libraries should be imported. If you only need a single function/class from the import, do an absolute import. It makes your code much cleaner, accurate, and easy to identify. Don’t forget to add a space between different types of imports.

There should be two blank lines surrounding classes and top-level functions. The methods inside of the class should be surrounded by a single blank line only. The preferred method of indentation is spaces, the 4 spaces indentation is accepted and accurate, but still, most people prefer tab indentation. Please keep in mind not to mix both spaces and tabs for indentation.

 

For Example:-


#

Don't forget to add a space between different group of imports

#

first of all, the standard library imports

import standard_library_import_a import standard_library_import_b

#

then,the third party imports

import third_party_import_a import third_party_import_b import third_party_import_c

#

at the last, local library import

from local library import local_a, local_b from local_library_two import local_c

#

two blank lines for top level functions

def top_level_function(argument):

#

A standard four space indent

print(argument)

B) THE LENGTH OF THE LINE AND THE LINE BREAKS

The length of the line should not be greater than 79 characters. In the case of docstrings and comments where a block of text is large, it is limited to 72 characters. For long multiple case statements, the backslashes are permissible. For using log statements with binary operators, python suggests breaking the formula line before the binary operator for better readability.

 

For Example:-


def sample_function 

(arg1, arg2):

'''

The document string length for a single line should be less than

72

characters. So that long texts should be adjusted in a single

window

'''

 

#  

code has maximum lengths of

79

characters, can use backslash

#

to break the line

list_of_subjects

= [

'

Physics

', '

Chemistry

', '

Mathematics

', '

Biology

',  ‘

Bio

’, \
]

   
2. WHITESPACES, TRAILING COMMAS, AND STRING QUOTES

One should avoid extra white spaces, there must be a single white space around both sides of an operator, one after the comma and none inside opening or closing of parenthesis. Both single quotes and double quotes are acceptable in python web development, you should use both if you need quotes inside quotes to avoid syntax error and extra backslash.
 

For Example:-


#

Examples of commas and whitespaces

x, y =

30

, "

text inside quote

"

z=

'

text inside quote

'

if x==

30

: print(x, y, z)

#

how to use quotes inside quotes

text =

"This text is using 'the single quote' inside double quote"

print(text)

3. NAMING CONVENTIONS

Use grammatically correct variable names, the class name should start with an uppercase and must follow camelCase convention If more than two words are to be used. In the same way, a function name should be joined with an underscore, and it must be lowercase. In method arguments, always use self as the first argument to declare an instance variable. In the same way, use ‘cls’ for the first argument for the class method. If the function name clashes with a reserved argument, use an underscore instead of a wrong spelling. Constants are declared in all capital letters.
 

For Example:-


#

class name follows camelcase convention

class

StudentDetails:

def __init__

(self, first_name, last_name):

self.first_name

= first_name

self.last_name

= last_name

#

Method name, variable names in lowercase joined with an underscore

def grade

(self, marks_obtained):

#

constants in capital

GRACE

=

2

marks_obtained

=

GRACE

+

marks_obtained

if marks_obtained

>

90

:

self.student_grade

= 'A'

elseif marks_obtained

>

70

:

student_grade

= 'B'

else:

student_grade

= 'C'