Skip to main content

Python : Basic Conventions

While we write any code, the predominant matter to be noticed is that the readability of our code is very much important. Even if our code is logically correct, it is not considered to be perfect if it is difficult to understand. It is said that our code is read much more often than its written. So here are some very basic conventions to be followed while writing any python program.

Python Enhancement Proposal(PEP) 8 describes the style guide for python code. Reading The Zen of Python will be interesting as well as informative before you move on.

Indentation
  • Use four spaces per each indentation level
  • Preferably use only white spaces for indentation and never mix up tabs and spaces
  • Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent.
  • Limit the maximum number of characters to be 79 in a line.
                 
                 def function(count)
                  if count > 100:
                      count += 10
                  return count

                                
Use of blank lines
  • Separate top-level function and class definitions with two blank lines.
  • Separate method definitions inside a class with single blank line.
  • Extra blank lines may be used to separate groups of related functions.
  • Imports should be on separate lines and are grouped as below:
                         standard library imports
                         related third party imports
                         local application/library specific imports
  • Each group of imports must be separated by one line.

Use of white spaces
You must avoid the use of white spaces in following cases:
  • Immediately before a comma, semicolon, or colon.
  • Immediately inside parentheses, brackets or braces.
  • Immediately before the open parenthesis that starts the argument list of a function call.
  • Immediately before the open parenthesis that starts an indexing or slicing.
  • More than one space around an assignment operator to align it with another.
  • Around the = sign when used to indicate a keyword argument or a default parameter value.
Always surround binary operators with a single space on either side.
Don't use spaces around the = sign when used to indicate a keyword argument or a default parameter value. 

                 squares = [1, 4, 9, 16]
                 sum = 0
                 for num in squares:
                     sum += num
                 print sum


Docstrings and Comments

Docstring explain how to use the code, and are for the users of your code. Comments explain why, and are for the maintainers of your code. Comments should be helpful to understand the codes and must not contradict the meaning.
  • In block comment, each line of a block comment starts with a # and a single space.
  • Paragraphs inside a block comment are separated by a line containing a single #.
  • An inline comment is a comment on the same line as a statement.
  • Inline comments should be separated by at least two spaces from the statement.
  • Inline comment should start with a # and a single space.

Naming Conventions

Never use the characters 'l'(capital eye), 'O'(capital ow) or 'I'(small el)  as single character variable names.

The commonly used naming styles are listed below

  • joined_lower for functions, methods, attributes
  • joined_lower or ALL_CAPS for constants
  • StudlyCaps for classes
  • camelCase only to conform to pre-existing conventions
  • Attributes:interface, _internal, __private

Also note that:
  • Modules should have short, all-lowercase names.
  • Almost in all cases class names use the CapWords convention.
  • Exceptions names follow the class naming convention. You should use the suffix "Error" on your exception names.
  • Function names should be lowercase, with words separated by underscores as necessary to improve readability.
  • Constants are usually defined on a module level and written in all capital letters with underscores separating words.



These are some very basic and strictly followed conventions while writing a python program. Many more details are available here and will surely help you to become a good programmer.

Comments

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Elements of Programming in Scheme

A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this. primitive expressions , which represent the simplest entities the language is concerned with, means of combination , by which compound elements are built from simpler ones, and means of abstraction , by which compound elements can be named and manipulated as units. Some very basic ideas in manipulating with the data are given below. Expressions   A very simple expression is a number. Simple expressions are combined to form complex ones. For example:                   (+ 2  3  4) is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:              ...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...