Python Coding Style Guide



In this tutorial, we are going to learn about the standard style guide that should follow in a Python project. Following a standard style guide of any programming language will keep all team members at the same pace. Moreover, it looks professional.

For Python, most of the users follow PEP 8 style guide. The code looks pretty and more readable. You can find the full list of style guide here. We are presenting the curated list of a style guide in this article.

Use a tab for indentation

Using a tab for indentation in the code make code more readable instead of using random spaces for multiple functions and methods. You can set the number of spaces for a tab in any code editors' settings.

You can see some examples below.

# example
def sample(random):
   # statement 1
   # statement 2
   # ...
   return random

Pythons' default UTF-8 or ASCII encoding

Using Pythons' default UTF-8 or ASCII encoding is the best practice for international environments.

Trailing comma in tuples

Using a trailing comma in the tuples is one of the best practices. But, it's not mandatory.

# example
tup = (1, 2, 3,)

Using docstrings

Use docstring in the functions and classes. We can use the triple quotes for the docstrings. See some examples below.

def sample():
   """This is a function"""
   """
   This
   is
   a function
   """
class Smaple:
   """This is a class"""
   """
   This
   is
   a class
   """

Don't write more than 79 characters in a line

Writing more than 79 characters in a line is not recommended in PEP 8 style guide. Avoid this by breaking the line into multiple lines using the escape character (). See the example below.

# example
def evaluate(a, b, c, d):
   return (2 ** (a + b) / (c // d) ** d + a - d * b) \ - (3 ** (a + b) / (c // d) ** d + a - d * b)

Using spaces

It's one of the best practices to use a space before and after an operator. Use a space after the comma as well for more readability.

# example
import random
result = random.randint(1, 3) + random.randint(1, 2)

Naming Variables, Constants, Classes and Functions

Follow the same case for variables, constants, classes and functions across the program. Most of the Python users will use snake_case for functions and variables naming and PascalCase for classes naming. For constants, use all uppercase letter separated with underscores (ex:- PI_TWO).

snake_case => this_is_function

PascalCase => ThisIsClass

CONSTANT => THIS_IS_CONSTANT

Importing one module at a time

Don't try to import multiple modules in a single even though it is syntactically correct. See the example below.

# don't
import math, random
# do
import math
import random

Comment updating

Always keep your comments up to date. Don't forget to update the comments while updating code. It's one of the most important things in coding. And most of the user will forget it. Keep this in mind.

Character that shouldn't be used as variable names singly

We have some characters that shouldn't be used as variable names lonely. And they are I (uppercase i), and l (lowercase L) as they look similar to the roman letters.

Don't use ASCII characters in identifiers

Using ASCII characters in identifiers is not at all a good practice. Avoid using them.


Advertisements