RidgeRun Developer Manual - Coding Styles - Python

From RidgeRun Developer Connection
Jump to: navigation, search




Previous: Coding Styles/C++ Index Next: Coding Styles/Javascript





Introduction to 'Python' Coding Styles

When writing Python code, RidgeRun tries to follow PEP8 - Style Guide for Python Code. Following we present some of the rules presented in this standard. For more information please visit PEP8.


Good practices

Be consistent with your code

In general, if you are adding code to an existent code, be consistent with the coding standard already being used. If no standard is used then follow this guide.


Code Layout

Indentation

Use 4 spaces per indentation level. See indentation for more information.

Maximum line length

Limit all lines to a maximum of 79 characters. See maximum line length for more information.

Imports

  • Imports should usually be on separate lines
# Correct:
import os
import sys
# Wrong:
import sys, os
  • Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
  • Imports should be grouped in the following order:
 
1. Standard library imports.
2. Related third party imports.
3. Local application/library specific imports.

You should put a blank line between each group of imports.

For more information see imports

Naming

Comments

Block comments

Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

Paragraphs inside a block comment are separated by a line containing a single #.

Inline comments

Avoid using inline comments.

x = x + 1                 # Inline comment, don't do it


# Do this instead
x = x + 1


Previous: Coding Styles/C++ Index Next: Coding Styles/Javascript