How to set up Python coding standards for a dev team (original) (raw)

Python team leaders have two challenges when it comes to code standards: creating a style guide and getting developers to stick to it.

Python's mix of syntactic and semantic rules make it a powerful computing language. But the code can be written in many ways to instruct computers to perform a given task. Coding standards make Python programs as efficient and effective as possible.

Coding standards help developers standardize their approach. The goal is code that is readable, maintainable and accessible to other humans and computers. Development teams typically have more than one programmer, and readability ensures all developers use code in expected ways. Standardized coding heightens communication between developers and gives the codebase scalability and maintainability benefits.

PEP 8 provides Python-specific coding standards for new programmers and those already fully immersed in the language. Get started with PEP 8 by learning its guidelines for naming conventions, indentation and comments. Then, improve your Python code more with tips for consistency in quotes, spaces, tabs, characters and other formatting choices. Finally, plan how to get the whole development team engaged with code standards.

What is PEP 8?

PEP stands for Python Enhancement Proposal, which is a style guide document about Python coding guidelines and best practices. PEP 0 was written in 2001 by Guido Van Rossum, Barry Warsaw and Alyssa Coghlan. PEP numbers are assigned by the PEP editors, and over time, new PEPs have come out with recommended Python style and design standards, through the current version 8. PEP 8's purpose is to ensure code consistency, quality and readability.

PEP 8 builds consistency within code structure and design. Consistency is an issue for projects overall, as well as for single code modules and functions. The more consistency in the code, the more readable it is to others. The more readable a codebase, the easier it is for other developers to maintain.

Basic Python formatting standards

PEP 8 indicates several common standards within Python. Standards also have exceptions, so exercise creativity when needed. To start following Python coding best practices, focus on naming conventions, indentation and comments.

Naming conventions

Python's library of naming conventions applies to code architecture parts and pieces to which developers assign names. Write all new modules and package names according to the following standards to create consistency and readability:

Indentation

Python uses four spaces for each indentation level. For continuation lines, wrap the elements vertically using Python line joining inside parentheses, brackets or braces using a hanging indent. With a hanging indent, don't use arguments on the first line, and use secondary indentation to distinguish a continuation line.

# Correct:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

(Credit: PEP 8 Style Guide for Python Code)

If the conditional portion of an if statement occupies multiple lines, use a two-character keyword plus a space, and add an opening parenthesis to create a four-space indent.

# No extra indentation.
if (this_is_one_thing and
    that_is_another_thing):
    do_something()

# Add a comment, which will provide some distinction in editors
# supporting syntax highlighting.
if (this_is_one_thing and
    that_is_another_thing):
    # Since both conditions are true, we can frobnicate.
    do_something()

# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
        and that_is_another_thing):
    do_something()

(Credit: PEP 8 Style Guide for Python Code)

Comments

Developers might call comments a necessary evil. Comments are essential to build a codebase that others can understand. Comments must be written in complete sentences. Review your comments to be sure they are clear and understandable. Ask another developer to interpret the comments. Did they understand your meaning correctly? If not, edit the comments.

Comments must be up to date. If you change the code, update the comments to match the current code function. To follow the PEP-8 guide, write Python code comments in English. Choosing one language for comments ensures transferability.

Python supports multiple comment types, depending on the code structure. Use block comments that are indented in the same manner as the code. Add a # followed by a single space to start comment text. Separate paragraphs within a comment using a single #. If helpful, use inline comments. These comments appear on the same line as a code statement. Separate inline comments from the code statement with two spaces. Start the inline comment with a # and a single space.

Other Python standards

Developers writing Python code should review PEP-8 and any updates to stay current. The following is a brief list of popular Python coding standards:

How do teams enforce coding standards?

Standards enforcement is a difficult subject and a delicate task. Nonetheless, code readability and consistency are cornerstones of the Python language, and to achieve them, developers must stick to the standards.

Development teams that implement and enforce coding standards are often more productive than those working to individual styles and preferences. Don't make coding more work than necessary. Consistent, readable code based on established standards saves everyone time and frustration. Additionally, it avoids defects that take hours to detect, troubleshoot and resolve because of individualistic and nonstandard coding practices.

Development teams should establish Python coding standards that work for the organization and the team. Leaders should start with a discussion or series of discussions on the topic of coding standards. Find out where team members disagree. Take the time to create a team coding standard using the Python standards. Developers are more likely to follow decisions that are transparent and take into account their suggestions.

During code reviews and other discussions, listen for inconsistencies or lax adherence to the standard. If you find developers aren't following the standards, to the detriment of the team's productivity and cohesiveness, hold a one-on-one discussion immediately. Don't let the code quality suffer before taking action.

Include the team in all Python coding standards and style guide updates to ensure agreement and transparency.

While code consistency is important, there are times when Python developers choose to break with PEP 8 standards. In some scenarios, style guides and standards complicate rather than help the codebase. PEP 8 encourages developers to go against the Python standards when they make the code less readable, break older code modules or prevent the code from being backward-compatible.

Amy Reichert is a 20-plus-year professional QA tester and a QA lead, specializing in test development, execution and management techniques. Her experience comes from a variety of sources, including ERP systems, architectural design, e-commerce and healthcare software.

Dig Deeper on Software design and development