Python Coding Standard


Python is a most useful language these days. If you are going to use it, its better to
follow particular standard so that it will be helpful for you and others as well in the future.
Here we are describing the coding standards that are followed by ML Team,
FCode Labs for their python products.

Naming Conventions

  • Class naming must be done with UpperCamelCase naming style.
  • All variables must follow snake_case naming style.
  • All constants must follow UPPER_CASE naming style.
  • All function naming must be done with snake_case naming style.
  • First argument of class methods must be self.
  • Private variable/method names should start with two underscores.
  • Protected variable/method names should start with single underscore.
  • All class variables (public, protected and private) must be defined inside the __init__() method or method called by the __init__() method. Make sure that this method to be private. Think why?? (hint: polymorphism)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
first_variable = 10

CONSTANT_VARIABLE = 20


class MyClass(object):
second_variable = 10

def __init__(self):
self.public_variable = 3
self._protected_variable = 4
self.__private_variable = 5

def public_method(self, arg_one):
return self.__private_method(arg_one + 1)

def __private_method(self, arg_one):
arg_two = arg_one * arg_one
return arg_two

Programming Rules

  • When testing for None, always use is
  • Do not use brackets in if-statement. (Only use in operator precedence)
1
2
3
4
5
6
7
8
9
10
11
# Do not use
if x == None and y != None:
pass

# Use this
if x is None and y is not None:
pass

# Can use brackets here
if (x + 1) / 10 == y:
pass
  • If a class has no base classes, then better to use object class as the base class.
1
2
class MyClass(object):
pass
  • Check for membership should use not in
1
2
3
4
5
6
7
8
9
a = [1, 2, 3]

# Do not use
if not 10 in a:
pass

# Use this
if 10 not in a:
pass
  • Imports should be grouped in the following order:
    1. Standard library imports.
    2. Related third party imports.
    3. Local application/library specific imports.
1
2
3
4
5
6
7
8
9
import os
import sys
import threading

import cv2
import tensoflow as tf

import zmlvideo
import mymodule

Documentation and Spacing

  • Doc-strings should be separated by three double quotes (""")
  • Spacing within a doc-string should be done as mentioned in the following example.
1
2
3
4
5
6
7
8
9
10
11
12
def some_function(arg1, arg2):
"""
You can describe the function in doc-string.
You can specity descriptions about argumets and return value.
You can set the return type also.

:param arg1: the first argument
:param arg2: the second argument
:return: the addition of two arguments
:rtype: int
"""
return arg1 + arg2
  • Should use 4 spaces to indent. (No tabs)
  • When assigning some value to a variable, one space should be used around the equal sign.
  • Every operator should be used between two spaces.
  • But when passing parameters or providing default argument values, do not use space around the equal sign.
  • Should use a space after every comma.
  • Functions and/or classes in a file should be separated with two new lines
  • Methods should be separated with only single new line.
  • Do not use more than one new line inside code body to separate code blocks.
  • Inline comments should have minimum 2 spaces from the end of the code line.
  • There should be at least a single space after the comment sign (#) and before the comment begins.
1
2
3
4
5
6
7
8
9
10
11
12
def some_function(arg1, arg2):
# There should be space around operators
return arg1 + arg2 # Minimum 2 space from the


class FirstClass(object): # 2 new lines after the function
def __init__():
self._value = self.__get_value(arg1=2, arg2=10)
# Other function should start after single new line

def __get_value(self, arg1, arg2=3): # No space around equal sign
return arg1 * arg2