Skip to content

Latest commit

 

History

History
401 lines (317 loc) · 7.98 KB

File metadata and controls

401 lines (317 loc) · 7.98 KB

Python Stylesheet

Python is an interpreted high-level programming language with dynamic and strong typing disciplines.

Layout

Whitespace and indentation

Use 4 spaces(U+0020) every indentation level.

Space(U+0020) is preferred instead of Tab(U+0009) for indentation.

If you use mixing indentation of spaces and tabs in Python 3, it will result a indentation error. In Python 2, tabs in the code will be converted to spaces.

The max line length should be limited to 79 characters (Excluding line feed and carriage return).

In this case, the proper format to include a extremely long name should be:

with open('/path/to/a/extremely/long/name/file\
           /that/you/want/to/read.txt') as file_1:
    # do something here
    file_1.close()

Separate lines of a long expression:

result = (calculate_result(param1, param2) - \
         100 * fixed_coefficient / \
         (large_int + another_large_int) + \
         addition_number)
# Or place operators after line break
result = (calculate_result(param1, param2)
         - 100 * fixed_coefficient
         / (large_int + another_large_int)
         + addition_number)

Imports also should be on separate lines:

import os
import sys

# However, it is OK to import several 
# methods from a library on single line.
from library import method1, method2

For line break,

Surround top-level function and class definitions with two blank lines. Surround method definitions single blank line.

# declare a function
def function1():
    pass
    
def function2():
    function1()
   
    
class no_name_class:

    def __init__(self):
        pass
        
    def method(self, param):
        pass
        

Comments

There are two ways to comment a line in python Starts with a # - # This is a line of comment or surrounds with triple " - """ This is a line of comment """

Block comments

# Describe what we want to do next, 
# never describe the code itself
# eg. We use method to find out 
# the largest number in the array

def method(param):
    pass

Inline Comments

A least two spaces from the statement.

x = (x + 1) & (y - 1);    # Increment on x or reset to 0 if x >= y

Documentation Strings

Docstrings are the comments to describe methods, functions and classes.

def merge_sort(arr, low, high):
    """Merge sort on array
    
    Detail information to describe the merge sort.
    
    Args:
        arr: an array to sort.
        low: start index of the array.
        high: end index of the array.
    Returns:
        A sorted array. 
        e.g.
        ......
        
    Raises:
        some_typeof_error: in what cases will the error occurs.
    """
    pass    # Method goes here
         
         
class SampleClassName(object):
    """Briefly describe the class
    
    Detail
    ...
    
    Attributes:
        
    """

Naming Conventions

Package and module names

# Lower case words with underscores (snake case)
import package_name
import module_name

Class names

# Upper case words combined (camel case)
class ClassName(object):
    
    __init__(self):
    pass

Exception names

# Basically same as class names
class ExceptionName(Exception):

    pass

Global variable names

global global_var_name

Function and variable names

variable_name = 123

def function_name():
    pass

Function and method arguments

class ClassName:
    
    def instance_method(self, ...):
        pass
    
    @classmethod
    def classMethod(cls, ...):
        pass
    
    @staticmethod
    def staticMethod():
        pass
        

Method names and instance variables

def function_name(method_parameter_name):
    pass

Constants

# All capital letters with underscores
CONSTANT_NAME = 123

Inherent methods

# Includes __new__, __init__, __str__ ...
class ClassName(object):

    def __init__(self):
        pass
    
    def __str__(self):
        pass
        

Statements

Conditional statements

if some_conditions:     
    # some_conditions must return a boolean value
    pass
else:
    pass   

if statement can have multiple elif and else is optional.

x = int(input("Please enter an integer: "))
if x > 0:
    print("Your input is positive")
elif x < -1:
    print("Your input is less than -1")
elif x < 0 and x >= -1:
    print("Your input is less than negative and larger than -1")
else:
    print("Your input is 0")

Loop statements

for statements:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
    print(number)
    
# Output is   
"""
1
2
3
4
5
"""
while statements:
while some_conditions:
    # some_conditions must return a boolean value
    do_something()

In Python, there is no do-while loop, but you can implement it like this:

while True:
    do_something()
    if fail_condition:
        break

Exception handling statements

try:
    do_something()
except IOError:
    print("IOError occurs")
else:
    print("No IOError occurs")
finally:
    do_something_finally()
    # THIS WOULD ALWAYS BE EXECUTED

Raise an exception:

raise ExceptionName(Error)

Declarations

Variables

integer = 1
float_number = 0.12345
pi = 3.1415
string = 'Hello'
object = function   # function object

global global_val   # a global variable
global_val = 123

Methods and functions

class ClassName:

    def method(self):
        pass


def function():
    pass
    
# Call method and function
sample_class = ClassName()
sample_class.method()

function()

Constants

CONSTANT_STRING = 'abc'
CONSTANT_INT = 123

Classes

class ClassName(object):

    def __init__(self, param):
        self.param = param
        
    def method(self, param1, param2):
        return param1 + param2
        
    def __str__(self):
        return 'This is a class'
        

Patterns

Wildcard pattern

for _ in range(5):
    do_something()

Underscore (_) variable means throwaway in Python.

String pattern

str1 = '%s, %d!' % ('Hello', 12345)
# str1: 'Hello, 12345!'
str2 = 'language: {}, version: {}'.format('Python', 3.6)
# str2: 'language: Python, version: 3.6'
string = str1 + str2
# string: 'Hello, 12345!language: Python, version: 3.6'

It is better to use """ for multiline strings instead of '''.

# first way
print("This is a multiline string.\n"
      "This is the second line.\n")

# second way
print("""This is a multiline string.
This is the second line.
""")

Though they produce the same outputs, the first one is better in this case.

List and Dictionary pattern

empty_list = []

integer_list = [1, 2, 3, 4, 5]
string_list = ['a', 'b', 'hello']
mixed_list = [1, 'hello', 1.5, (1, 'tuple'), ['banana', 1000]]

matrix = [
    [1, 2, 3, 4],
    [2, 4, 6, 8],
    ]

Dictionary in Python is similar to JSON object.

dictionary = {}     # an empty dictionary
example_small = {'key': 'value', 'number': 2}
example_large = {
    'key1': 'value1',
    'key2': 'value2',
    ...
                
}

Regular expression

  • . matches any character except a newline.
  • ^ matches the start of the string.
  • $ matches the end of the string.
  • * matches 0 or more repetitions of the preceding regular expression.
  • + matches 1 or more repetitions of the preceding regular expression.
  • ? matches 0 or 1 repetitions of the preceding regular expression.

References

  1. PEP 8, Style Guide for Python Code, van Rossum
  2. Naming convention (programming) - Wikipedia
  3. Google Python Style Guide, Amit Patel, et al.
  4. Regular expression operations - The Python Standard Library