support Click to see our new support page.
support For sales enquiry!

Python PEP8 Coding Standard

python pep8 coding

HibaSept. 7, 2018

Python is very easy to learn than most of the programming languages. While comparing with other programming languages, you only have a relatively low number of keywords to internalise in order to write correct Python code. Both readability and simplicity of the code makes python really awesome. Moreover, PEPs are Python Enhancement proposals, and they describes and document the way python language evolves. PEP8 is a set that contain rules of best coding practices that need to be followed. If those practices and conventions are not followed, PEP8 will give errors. However, the quality of Python PEP8 Coding Standard makes it really distinct from others.

It is not a comprehensive style guide but a basic minimum requirement for python code. And also it is not a requirement for your code to work, just a good coding practice you should follow. It doesn't have to memorize all of it by heart, since there are many command line tools to check if your code is compliant with the PEP8 style guide. The following blog will give you a detailed view of Python PEP8 Coding Standard.

Why it is needed and what are the features?

  •  Provide pythonic way to write the code.
  •  Aim to improve readability of the code.
  •  Enforce same standard for all developers.

Consistency as a basic rule

  • Consistency is very important when working with in a project. Make sure that all who take part in project follow the same style.

Code Lay-out
1. Maximum Line Length

  •   Limit all lines to a maximum of 79 characters.

2. Indentation

  • You have to leave 4 spaces for each level of indentation.

Python PEP8 Coding Standard

  •  Tabs or Spaces?
  • Spaces are most preferred than Tabs.
  • Python 3 code does not allow mixing the use of tabs and spaces for indentation.
  • Python 2 code indented with spaces and tabs should convert to use spaces exclusively.

3. Blank Lines

  •  A good practice to follow is separate top level functions and classes by two blank lines than rest of code.
  •  Method definition inside a class are surrounded by single blank line.
  • Use blank lines in function sparingly to indicate logical section.
  • Extra blank lines may be used (sparingly) to separate group of related functions.

4. Using spaces in expressions and statements

  •  The rules regarding spaces in expression and statements are simple to understand and follow.

a. Firstly, avoid white space immediately before or after parenthesis, brackets or braces and leave space immediately after commas, semicolon and colons.

Example for Using spaces in expressions and statements

b. In expression there must be only one white spaces before and after the operator.

In expression there must be only one white spaces before and after the operator

c. The assignment operator (=) do not want spaces in function statement where a default value is assigned to an argument.

Example for the assignment operator, do not want spaces in function statement

d. After that, avoid space between a trailing comma and following closing parenthesis.

Example for Avoiding space between a trailing comma and following closing parenthesis

e. Always keep these binary operators (Like Assignment,Argument assignment,Comparison, Boolean ) with single space on either side.

f. Multiple statements on the same line are generally discouraged.

5. Should a line break before or after the binary operator?

  • Line break after the binary operator is more recommended.

line break before or after the binary operator in Python PEP8 Coding Standard

6. Imports

  •  Import should usually be on separate line

Imports in Python PEP8 Coding Standard

  • Imports are always at the top of the file just after any module comments and doc strings.
  • It grouped as standard library, Related third party and local application imports. Use one blank line to separate each group.

7. Use Docstrings

  •  There are both single and multi line docstrings.
  •  Closing triple quotes (” ” ” ”) for one liner docstring will be on the same line and
    for multi line docstring it should be on a line by itself.

8. Source file encoding

  •  Use python's default UTF-8 (python 3) or ASCII (python 2) encoding and do not any other fancy encoding.

9. When use trailing commas?

  •  Usually trailing commas are optional and uses when making a tuple.

10. Commands

  •  Add commands to increase readability of your code.
  •  Since it is a written text first letter should be capitalized.
  •  Keep comments up-to-date when code changes.
  •  There are Block commands and in line commands.
  •  Block commands: It is a descriptive lengthy commands before defining class, module, or method. Here   each line in the command starts with a # sign followed by a single space. The paragraphs also separated by a bank line which is also begin with a # sign.
  •  In line commands : It is a comment on the same line as a statement. and use in-line commands sparingly. It should be separate at least two space from the statement and also starts with a # followed by single space.

Line commands Python PEP8 Coding Standard

11. Naming conventions

  • Lower case 'l' , upper case 'O', upper case 'I' never used as a single variable name since in some font they are indistinguishable from numeral one and zero.

Naming conventionsBased on the Python PEP8 Coding Standard you can start formatting your code correctly to maximize it's readability and efficiency. Hope you will follow Python PEP8 Coding Standard and maintain the pythonic way.....


LinkedIn LinkedIn