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
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
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
Post a Comment