What is meant by PEP 8 style guide?Give suitable example.
What is meant by PEP 8 style guide?Give suitable example.
670
28-Mar-2023
Krishnapriya Rajeev
03-Apr-2023PEP, or Python Enhancement Proposal, refers to several documents that describe new features that are proposed for Python and document aspects of Python, such as design and style. These documents are released to the developer community.
PEP 8 (also called PEP8 or PEP-8) is a PEP document that provides guidelines and the best practices for writing Python code. Written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan, the main focus of PEP 8 is to improve the readability and consistency of Python code.
A few of the key guidelines to be followed according to PEP 8 are:
Choosing sensible names that will save you time and energy later. For example, avoiding single-letter names like l and O is more efficient, as these can look similar to 1 and 0 which may confuse.
2. Naming Styles
3. Code Layout
a. Using blank lines:
Surround top-level functions and classes with two blank lines.
Use blank lines sparingly within the functions to show clear steps. This can be done at the programmer's discretion
b. Maximum Line Length:
PEP 8 suggests lines be limited to 79 characters in length to allow users to have multiple files open next to one another while avoiding line wrapping.
c. Indentation:
The key indentation rules laid out in PEP 8 are the following:
Use 4 consecutive spaces to indicate indentation.
Prefer spaces over tabs.
d. Lining up the Closing Brace:
Line up the closing brace with the first non-whitespace character of the previous line.
e. Comments:
One should limit the line length of comments and docstrings to 72 characters. Make sure to use complete sentences, starting with a capital letter. Do not forget to update comments if you change your code.