Wednesday, August 8, 2012

Python Style Coding


Usually we start our programming through languages like C, C++ or Java. Now if we also want to use Python, it gets a bit tricky. It is a truth that a code is read more often than it is written. So it becomes important to follow certain conventions to maintain consistency and readability (at least in python).

Python being a dynamically typed language does not notify us about the programming errors unless it is run. So with the sheer size of the code, consistency becomes mandatory. (Of course unit testing can be done, but still there is a danger of flawed code running fine; and as always prevention is better than cure).

The two main things that should be kept in mind  about the code layout are the use of indentation levels and white spaces. There is something called The Zen of Python in PEP, if you like to read (it's better to have some reading habits).

Indentation
Unlike most other languages white space indents are an important part of Python. It recognizes indents as a substitute for braces.  But this may get a little messy while writing code of the size of an elephant.

The official Python style guidelines  says that we must indent with 4 spaces but some others like Google dictates indenting by 2 spaces. The use of tabs must be strictly avoided although some editors provide provision to set 4-space or 2-space tabs instead of the conventional 8-space tabs.

Apart from that, the most import thing is never to mix tabs and spaces. It could cause a lot trouble while editing (endurance could become a problem).

def function():
    if condition:
        statement 1
        .......
        ...

def func():
    global i
    i += 1
    return


White Spaces
Although white spaces are not a necessity in Python, as they say the readability counts, the wise use of white spaces and blank lines are needed to ensure it.
The official Python style suggests the use of same amount of spaces before and after arithmetic operators (except within function arguments). It also suggests the use of a space after a coma, colon or a semicolon (like in IEEE format).

i = i + 1
def fun(a, b=0):
    ...........
    ....
    callanother(c=1, d=2)

The use of spaces before and inside parenthesis, spaces in array or dict indices (like array [1]) must be avoided.

Use of blank lines are encouraged before and after logical function bodies or parts. A single blank line is recommended for separating functions and a double blank line for separating classes.

Other conventions

  • maximum line length of 79 characters.
  • wise use of comments improve understanding.
  • use of version bookkeeping and documentation strings.
  • naming styles (see here)
  • import statements at the top of the code in the following order with a new line for each import
    • standard library imports
    • related third party imports
    • local application/library specific imports
There are many programming recommendations[1, 2 ] exclusive for Python (Python Enhancement Proposals, PEP) which helps us building more productive codes in less time.


No comments:

Post a Comment