PEP 8 Python

Share

                    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:

  1. Indentation: Use 4 spaces per indentation level. Continuation lines should align wrapped elements or use a hanging indent.

  2. Line Length: Limit all lines to a maximum of 79 characters for code and 72 for comments and docstrings.

  3. 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.

  4. 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.

  5. Whitespace: Avoid extraneous whitespace in expressions and statements.

  6. 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).
  7. Comments: Use complete sentences and keep them up to date with the code.

  8. 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.""").

  9. Operators and Operands: Put spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

  10. Comparisons to Singletons: Use is or is not for comparisons to singletons like None.

  11. 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.

  12. 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

 
You can find more information about Python in this Python Link

 

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


Share

Leave a Reply

Your email address will not be published. Required fields are marked *