Skip to main content

Python 3 Style Guide

⚡ View the sidebar (right) for the rapid navigation.

❗ We follow PEP8 Guidelines and most of the Google Python Style Guide. The below guidelines listed below.

💂‍♂️ Formatters & Linters

We use husky for pre-commit hooks.

Husky will run the following when you try to commit:

Any errors must be resolved prior to committing code.

You can run these yourself as well:

yapf -i -r -vv . style google
flake8 .

❗ These checks will also be run for pull requests. All errors must be resolved.

💬 Naming Conventions

See Google Style Guide Conventions.

In short: ClassName, function_name, CONSTANT_NAME, ExceptionName, local_var_name

📔 Docstrings

"Docstrings" are comments containing critical information about classes and functions.

The following conventions for Python 3 docstrings shall be adhered to.

  1. class docstrings should contain:
    • A description of the class
    • list of class attributes (initialization parameters)
  2. function (def) docstrings should contain:
    • A description of the function's purpose
    • Arguments to the function
    • The expected return value (and type) of the function

Example:

class ComplexNumber:
"""
This is a class for mathematical operations on complex numbers.

Attributes:
real (int): The real part of complex number.
imag (int): The imaginary part of complex number.
"""

def __init__(self, real, imag):
"""
The constructor for ComplexNumber class.

Parameters:
real (int): The real part of complex number.
imag (int): The imaginary part of complex number.
"""

def add(self, num):
"""
The function to add two Complex Numbers.

Parameters:
num (ComplexNumber): The complex number to be added.

Returns:
ComplexNumber: A complex number which contains the sum.
"""

re = self.real + num.real
im = self.imag + num.imag

return ComplexNumber(re, im)

help(ComplexNumber) # to access Class docstring
help(ComplexNumber.add) # to access method's docstring

(from Geeks for Geeks)

📃 License Notice

Every file should start with a license notice.

The license may vary from repository to repository.

Check with the #legal team if unclear which license to use.