PEP 8 Python
PEP 8 is the style guide for Python code. It’s a set of rules for how to format Python code to ensure that it is easy to read and consistent across different projects. Following PEP 8 can make it easier for other developers to understand and work with your code. Here are some of the main points:
-
Indentation: Use 4 spaces per indentation level. Continuation lines should align wrapped elements or use a hanging indent.
-
Line Length: Limit all lines to a maximum of 79 characters for code and 72 for comments and docstrings.
-
Blank Lines: Surround top-level function and class definitions with two blank lines. Method definitions inside a class are surrounded by a single blank line.
-
Imports: They should usually be on separate lines and placed at the top of the file. The order is standard libraries, third-party libraries, and then your own local applications.
-
Whitespace: Avoid extraneous whitespace in expressions and statements.
-
Naming Conventions:
- Functions should be lowercase, with words separated by underscores (e.g.
my_function
). - Constants are usually defined on a module level and written in all capital letters with underscores separating words.
- Class names should follow the CapWords convention (e.g.
MyClass
).
- Functions should be lowercase, with words separated by underscores (e.g.
-
Comments: Use complete sentences and keep them up to date with the code.
-
Docstrings: Write docstrings for all public modules, functions, classes, and methods. They should be written in triple double-quotes (e.g.
"""This is a docstring."""
). -
Operators and Operands: Put spaces around operators and after commas, but not directly inside bracketing constructs:
a = f(1, 2) + g(3, 4)
. -
Comparisons to Singletons: Use
is
oris not
for comparisons to singletons likeNone
. -
File and Encoding: Python files should be encoded in UTF-8 and start with the standard shebang line if they are meant to be executable.
-
Module Level Dunder Names: Module level “dunders” (i.e. names with two leading and two trailing underscores) like
__all__
,__author__
, etc., should be placed after the module docstring but before any import statements.
PEP 8 is widely followed, and many editors and IDEs have plugins or features that can automatically format code according to these guidelines. There are also command-line tools like autopep8
or black
that can automatically reformat code to conform to PEP 8. By following these guidelines, developers contribute to maintaining a consistent and readable code style throughout the Python community.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks