Python Q & A

 

What is the PEP 8 standard for Python?

PEP 8, often simply referred to as “PEP 8”, stands for Python Enhancement Proposal #8. It’s a coding convention, a set of recommendations on how to write Python code more readable and consistent. Adopting these standards makes the code understood and maintained by a larger group of developers, a vital aspect for collaborative projects. Here’s a brief overview:

 

  1. Formatting:

   – Use 4 spaces per indentation level, and don’t use tabs. 

   – Limit all lines to a maximum of 79 characters for code, and 72 for comments.

   – Use blank lines to separate functions, classes, and blocks of code inside functions.

 

  1. Naming Conventions:

   – Use `lowercase_with_underscores` for function and variable names.

   – Use `CamelCase` for class names and `UPPERCASE_WITH_UNDERSCORES` for constants.

 

  1. Expressions and Statements:

   – Avoid using extraneous whitespace in the following situations: `{a: 1}` is preferred over `{ a: 1 }`.

   – Always use `def` instead of assigning a lambda expression to a variable.

   – Check for empty values by using `if not some_list` and `if some_list` rather than comparing lengths.

 

  1. Imports:

   – Always put imports at the top of the file.

   – Imports should be grouped in the following order: standard library imports, related third-party imports, local application/library specific imports. 

   – Each group should be separated by a blank line.

 

  1. Comments:

   – Comments should be complete sentences and should be used sparingly, i.e., only when necessary to explain complex pieces of code, decisions that could seem non-obvious to other developers, etc.

It’s worth noting that PEP 8 is just a guideline, not a strict rulebook. While it’s widely adopted in the Python community, there might be situations where deviating from PEP 8 is justified. Nevertheless, understanding and generally adhering to PEP 8 is considered a mark of a professional Python developer. For full details, one should refer to the official PEP 8 document.

Previously at
Flag Argentina
Brazil
time icon
GMT-3
Senior Software Engineer with 7+ yrs Python experience. Improved Kafka-S3 ingestion, GCP Pub/Sub metrics. Proficient in Flask, FastAPI, AWS, GCP, Kafka, Git