콘텐츠로 건너뛰기

통찰력 있게 PEP8을 사용하여 Python 코드 작성 방법과 수정 방법!

[

PEP 8: Pythonic Code Guidelines

PEP 8 is a set of guidelines and best practices that determine how Python code should be written. These guidelines aim to improve the readability and consistency of Python code. By following PEP 8, you can write code that is easier to understand and maintain.

Why We Need PEP 8

The Zen of Python states that “readability counts,” emphasizing the importance of writing code that is easy to understand. Code is read more often than it is written, so it is crucial to prioritize readability. By following PEP 8, you ensure that your code is clear and comprehensible to both yourself and others.

Naming Conventions

Good naming conventions make code more readable and understandable. PEP 8 provides guidelines for naming variables, functions, and classes. It suggests using lowercase with words separated by underscores for variables and functions, and using CamelCase for class names.

Naming Styles

PEP 8 recommends using the following naming styles:

  • lowercase_with_underscores for variables and functions
  • CamelCase for classes

How to Choose Names

When choosing names for variables, functions, and classes, it is important to use descriptive and meaningful names. PEP 8 suggests using names that convey the purpose or intent of the entity being named. This ensures that the code is self-explanatory and easy to understand.

Code Layout

Code layout refers to the arrangement of code elements, such as indentations, line breaks, and blank lines. Following proper code layout makes code more readable and organized.

Blank Lines

PEP 8 recommends using blank lines to separate functions, classes, and sections of code within a function or class. This improves code readability by visually separating different parts of the code.

Maximum Line Length and Line Breaking

PEP 8 suggests keeping lines of code within a maximum length of 79 characters. If a line exceeds this limit, line-breaking techniques such as using parentheses or backslashes can be used to split the line and improve readability.

Indentation

Indentation plays a crucial role in Python code, as it defines the structure and hierarchy of the code blocks. PEP 8 provides guidelines for indentation practices.

Tabs vs Spaces

PEP 8 recommends using spaces for indentation instead of tabs. Spaces ensure consistency across different platforms and text editors.

Indentation Following Line Breaks

When a line of code is too long and requires line breaking, the continuation line should be indented by four spaces to maintain readability.

Where to Put the Closing Bracket

For multiline constructs, such as function definitions or class definitions, the closing bracket should be placed on a separate line, aligned with the indentation level of the opening bracket. This improves code readability and makes it clear where the construct ends.

Comments

Comments are used to explain the code and provide additional information to the reader. PEP 8 suggests guidelines for writing comments.

Block Comments

Block comments are used to explain larger sections of code or provide a brief overview of the code’s functionality. They should be indented to the same level as the code they are describing and should start with a # followed by a single space.

Inline Comments

Inline comments provide explanations for specific lines of code. They should be used sparingly and should be separated from the code by at least two spaces.

Documentation Strings

Documentation strings, or docstrings, are used to document functions, classes, and modules. PEP 8 provides guidelines on how to format docstrings to make them more useful and readable. Docstrings should use triple quotes and follow a specific format to ensure consistency.

Whitespace in Expressions and Statements

Proper use of whitespace in expressions and statements improves code readability. PEP 8 provides guidelines on when and how to add whitespace.

Whitespace Around Binary Operators

PEP 8 suggests adding a single space before and after binary operators, such as +, -, *, /, etc. This enhances the readability of the code and makes it easier to understand the operations being performed.

When to Avoid Adding Whitespace

There are cases when adding whitespace can be avoided, such as between a function name and its parentheses, or between a variable name and its index or attribute access.

Programming Recommendations

In addition to style guidelines, PEP 8 also provides programming recommendations. These recommendations cover various aspects of Python programming, including exception handling, importing modules, and handling mutable default arguments.

When to Ignore PEP 8

While following PEP 8 is generally recommended, there are situations where it may be appropriate to deviate from the guidelines. PEP 8 acknowledges that readability may vary depending on the context and purpose of the code. In such cases, it is acceptable to prioritize clarity and functionality over strict adherence to PEP 8.

Tips and Tricks to Help Ensure Your Code Follows PEP 8

To make it easier to write PEP 8 compliant code, there are several tools and techniques available. These include linters, which check your code for style violations, autoformatters, which automatically format your code to follow PEP 8, and combined tooling, which provides both linting and autoformatting functionality.

Conclusion

Following PEP 8 guidelines is essential for writing Python code that is easy to read, understand, and maintain. By adhering to these guidelines, you ensure that your code is consistent, well-formatted, and accessible to both yourself and other developers.