Python Rules and pep8 Guidelines:
Hello Readers, welcome back to know the Python Rules and PEP8 Guidelines. PEP8 is a Python Enhancement Proposal Documentation to maintain the code consistency and readability throughout the Project or Application. These guidelines help us to improve the writing style of Python code and provides the reusability of the components. Python scripting can be written in three different ways like Scripting, Functional Scripting and Object Oriented.
This style guide is used well in the industry while developing the complex projects. Consistency within the project is more important because the things might change in an Agile manner. So to keep track within the project and module level, consistency is more important.
PEP8 Python Rules:
It defines the continuation of the next line with the previous line in a vertical direction and in implicit line parenthesis, braces and brackets. Indentation uses 4 spaces if not it raises a syntax error. Continuation lines exist while looping statements, conditional statements, function definitions.
Tabs or Spaces:
Spaces are preferred more in Python 2.7. Spaces are also often used in Python 3. Once Tabs are used in the python code, it should be used entirely if not, you get indentation error. Python 3 doesn’t allow the mixing of Tabs and Spaces using in the Python code. Please be careful while using Tabs in the code.
Maximum Line Length:
Generally, coders use to write the long lines as a result readability will be decreased and looks clumsy. So Pep8 standard is to write 79 characters maximum in one single line. But you can use 79 to 99 characters in one single line and it depends completely on developers choice. Please try to maintain below 99 characters.
Line Break using Binary Operators:
It is written when we use mathematical calculations in the program. As we all know the operator precedence rules to get exact results. Line Breaks are used in the Binary Operators. Line Break must be used before the operands which define more precise to understand by computers.
income = (gross_wages + taxable_interest + (dividends - qualified_dividends))
Blank Lines are used to differentiating logical code with other lines. One blank line is used for method definitions inside the class to separate. Two blank lines are used for class definitions.
Imports should be written in the top level statements with separate lines for each import statement in the file.
import sys import os
After any module comments or docstrings use the single separate line.
Import statements should be grouped in the following way:
- Standard library imports
- Related third-party imports
- Local applications/library specific imports
Once the related imports are done leave one single blank line to differentiate.
When importing a class from a class-containing module, it’s usually okay to write like this:
from myclass import MyClass from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them explicitly:
import myclass import foo.bar.yourclass
wildcard imports should be avoided because it makes confusion for both readers and automated tools.
from scikit import *
These are helpful when you use version control systems. examples list of values or dictionaries.
Whenever it is required use trailing commas in braces or brackets.
Files = [ 'setup.config', 'tox.ini', ]
Don’t use in this way:
FILES = ['setup.cfg', 'tox.ini',]
Generally, comments should start with a capital word as the first letter. This is usually used to declare multiple lines or more than one line. Block comments apply to some code that follows them continuously and are indented to the same level as that code.
Inline comments are used to declare as the same line comment. You can use ‘# Comment line starts.
You can also use this for local declarations or in loops.
Naming conventions are most important in coding where you follow the pattern and its reused many times. It provides the code readability and easy to understand.
General Naming Conventions follows as shown in the image:
Please click for the more information about Python Coding Conventions
Please click here for the Python Programming Advanced Features