Coding Style of Python- PEP 8
PEP expands as Python Enhancement Proposal which has emerged as the style guide which the most projects follows. PEP 8 is a simple way of coding and very readable coding style. This proposal establishes some of the key points which we can use to make our code more organized and readable.
In python, indentation is very necessary while coding. If we don’t take care of it, this will lead to syntax error. Generally, we use 4 spaces are used as indentation. This 4-space is not always necessary and can be overruled for next line or continuation line.
def function_name(variable_one, variable_two): print(variable_one)
Docstrings are the comments which are non-executable lines in the code used to understand the code. There are single and multi-line docstrings in Python. Single line comment can be used by hash, and multi-line comments can be used by triple quotes.
# This is a single line comment '''This is multiple line comment'''
3. Line Length
Maximum line length in a code is 79 characters in Python. due to this, we can able to open files side by side to compare. We can view the entire expression without scrolling horizontally. The lines are wrapped by using parenthesis, brackets and braces.
with open('/path/from/where/file/want/to/read', 'r') as file_1, \ open('/path/to/where/the/file/want/to/be/written', 'w') as file_2: file_2.write(file_1.read())
4. Blank Lines
In python, blank lines are used to separate top-level function and classes. We should use one blank line, so that method definitions inside classes are separated.
5. Regular and updated Comments
There are various types and conditions in the comments. Comments must form complete and meaningful sentences. First word should be an upper case in case of a comment, or else it would be an identifier. Period at the end can be ignored for short comments.
In block comments, more than one paragraphs are present which are ended by periods. Block comments and regular comments can be written by beginning with a single hash “#”.
name='Python' # Inline Comment # Single line Comment '''Multiple line Comment'''
6. Trailing Commas
Trailing commas are not necessary unless while creating a tuple.
Tuple= ("Python", "Java", "C")
7. UTF-8 or ASCII encodings
Use Python’s default UTF-8 or ASCII encodings are not any fancy encodings, these are specially meant for international environment.
Spaces are used around operators and after the commas. It cannot be used directly inside bracketing constructs.
z = x(a, b) + y(c, d)
9. Naming Conventions
10. Characters and Identifiers
Characters should not be used for identifiers. Because lowercase letter “L”, uppercase letter “I” and uppercase letter “O” resembles as numerals 1 and 0.
11. Non-ASCII characters
If there is only slightest chance of people speaking a different language will read or maintain the code, do not use non-ASCII characters in identifiers.
12. Naming classes and functions
Name the Classes with CamelCase and Name the functions with lower case, words are separated by underscores. First method argument should always name as self.