Refactoring Code To Conform With PEP 8 Style Guide: A Python Example

PEP 8 is the official style guide for Python code, maintained by the Python community. It provides guidelines and conventions for writing clean, readable, and consistent Python code. Following PEP 8 helps improve code quality and makes it easier for developers to collaborate on projects. Here are some key points from PEP 8, and it will provide some examples of Python code that using or not using PEP 8.

1. Python Code PEP 8 Key Points.

Indentation: Use 4 spaces per indentation level. Avoid tabs.

Maximum Line Length: Limit all lines to a maximum of 79 characters for code and comments. For long lines, break them using parentheses, backslashes, or other appropriate methods.

Imports: Import statements should usually be on separate lines. Imports should be grouped in the following order:

- Standard library imports
- Related third-party imports
- Local application/library specific imports

Whitespace in Expressions and Statements: Avoid extraneous whitespace in the following situations:

- Immediately inside parentheses, brackets, or braces.
- Immediately before a comma, semicolon, or colon.
- Immediately before the open parenthesis that starts the argument list of a function call.
- Avoid extraneous whitespace in assignments and comparisons.

Comments: Use comments to explain non-obvious code, but avoid over-commenting. Comments should be complete sentences and should be placed on a separate line if they are long.

Function and Variable Names: Use lowercase letters and underscores to separate words in function and variable names. Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names should be lowercase with words separated by underscores.

Constants: Constants should be in uppercase with underscores separating words.

Whitespace in Expressions and Statements: Avoid extraneous whitespace in expressions and statements.

Blank Lines: Use blank lines to separate functions, classes, and blocks of code inside functions. Two blank lines are used between top-level functions and classes.

Imports Formatting: Imports should usually be on separate lines, and each import should be on its own line.

String Quotes: Use single quotes for short strings and double quotes for longer strings. Triple-quoted strings are used for docstrings.

Coding Style: Consistency within a project is important. If the project already follows a certain style, stick with it.

Remember that these are just highlights from PEP 8. To get the most accurate and up-to-date information, it’s best to refer directly to the [PEP 8 style guide]. You can also use tools like linters, Flake8, to automatically check your code against PEP 8 standards.

2. Python Code Example Not Comply With PEP 8 Style Guide.

Here’s an example of Python code that does not comply with the PEP 8 style guide:

def   myFunction( x,y ):
return x+y
result=myFunction(5, 10)
print(result)

In this example, the function name `myFunction` should be `my_function` according to PEP 8’s naming conventions. The function arguments `x,y` should have spaces around the comma, like `x, y`.

The indentation for the `return` statement is incorrect; it should be indented with four spaces. The assignment `result=myFunction(5, 10)` lacks spaces around the equals sign, should be `result = my_function(5, 10)`.

The call to `myFunction` should be `my_function` and again, should have spaces around the arguments. The `print(result)` line is correctly formatted.

3. Python Code Example Comply With PEP 8 Style Guide.

Here’s the same code adjusted to comply with the PEP 8 style guide:

def my_function(x, y):
    return x + y
result = my_function(5, 10)
print(result)

In this revised code, the function name is changed to `my_function` to use lowercase with underscores for improved readability. The function arguments have spaces around the comma for clarity.

The `return` statement is properly indented with four spaces. The assignment and the function call have spaces around the equals sign and commas. The `print` statement is correctly formatted.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.