Python conventions

last update : March 31, 2011

Module names

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. Every Python module has it’s __name__ defined and if this is ‘__main__’, it implies that the module is being run standalone. The function called main doesn’t have any special significance in Python. However, it is common practice to organize a program’s main functionality in a function called main and call it with code similar to the following:

def main():
    # the main code goes here

if __name__ == "__main__":


There are two styles of comments in python :

# single line comments

" " " multiple


comments " " "

Multiline comments, enclosed by triple-quotes, are used to embed doc strings, for example in a class school. If you declare an object t=school and then t . __doc__ or t (help), the embedded comments are returned. Triple-quotes should not be used to comment out large blocks of Python code.

Self argument of methods

The first argument of a method is usually called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python.  Programs that are not following these convention are less readable to other Python programmers and may fail in class browser programs which rely upon such a convention.


In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character:


Raw Strings

Raw strings are written as strings prefixed with the ‘r’ or ‘R’ character. Raw strings don’t treat the backslash as a special character. Every character you put into a raw string stays the way you wrote it.



Indentation (leading whitespaces) in Python counts. Every block structure — controls, classes, functions, etc. — must be indented the same number of times for their level in the program. Convention is to use four spaces (without tabs) for each level of indentation.

Line continuation

To break a long statement into several lines, the line continuation character (backslash) can be used at the end of the breaked physical lines (explicit line joining). This syntax is however outdated and should be avoided. Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes (implicit line joining). A line ending in a backslash cannot carry a comment, but implicitly continued lines can carry comments. The indentation of implicit continuation lines is not important.

Django : high-level Python Web framework

Django was designed to make common Web-development tasks fast and easy. Django adheres to the DRY (Don’t repeat yourself) principle.

Django comes with an object-relational mapper in which you describe your database layout in Python code. Other useful features are a caching framework that integrates with memcached or other backends and a syndication framework that makes creating RSS and Atom feeds as easy as writing a small Python class.

Django has a rich documentation and great tutorials. An active community gives answers to common questions and hints to solve problems.

The Google App Engine provides a Python runtime environment including Django as a third-party library. I started yesterday evening to host a first Facebook application, designed with Python and Django,  on the Google App Server.

Django was originally developed at World Online, the Web department of a newspaper in Lawrence, Kansas, USA. Django is now run by an international team of volunteers.

The main elements of the Django Template Language are :

  • variables :  {{section.title}}   the content is replaced by the title attribute of the section object
  • filters : {{name|ower}}   the content is replaced by the lowercase string of the name; there are about 30 built-in filters; custom template filters can be created
  • tags :  {% doit %}  tags are more versatile than variables;  tags can be used for loops, conditions, …; there are about 24 built-in tags; custom template tags can be created
  • comments :  {#my comment#}  only for single-line comments
  • decorators : @my_decorator   – a decorator is a function that wraps the decorated function  in yet another function, allowing you to run code both before and after the main function
  • template inheritance : {%block identifier%} …….. {%endblock%}   the content of the block named identifier in the base template can be overridden by the content specified in child templates