目次
- 1 1. What is PEP 8
- 2 2. Variable Naming Conventions
- 3 3. Function Naming Conventions
- 4 4. Class Naming Conventions
- 5 5. Constant Naming Conventions
- 6 6. Naming Conventions for Modules and Packages
- 7
- 8 8. Special Naming Conventions (Dunder Methods)
- 9 9. Importance of Naming Conventions and Best Practices
- 10 10. Tools and Tips Useful in Practice
1. What is PEP 8
PEP 8 is the official style guide for keeping Python code consistent and improving readability. Especially in large projects or team development, following a unified set of rules makes code easier to understand and enhances maintainability.Key Rules of PEP 8
- Indentation: Use four spaces consistently. Using spaces instead of tabs ensures a uniform appearance across all editors and prevents confusion within the team.
- Line length: It is recommended to keep lines to 79 characters or fewer. This improves visibility in the editor and makes code reviews with multiple people easier.
- Blank lines: Insert two blank lines between top‑level functions or classes, and one blank line between methods inside a class, to keep the code organized and readable.
- Import order: Arrange imports in the order of standard library, third‑party modules, then local modules, with a blank line separating each group. This visually distinguishes the modules and makes the code easier to understand.
- Comments: Write comments consistently, concisely, and clearly, aiming to supplement the intent of the code.
2. Variable Naming Conventions
In Python, it is recommended to use snake_case for variable names. Snake case separates words with underscores (_) and uses only lowercase letters. This makes variable names easier to read at glance and clearly conveys their purpose. Good example:total_count
, user_name
, average_speed
Bad example: totalCount
, UserName
, AverageSpeed
Give Meaningful Names
It’s important to choose variable names that reflect their content and purpose. For flags or state variables, adding prefixes like “is_” or “has_” makes their role even clearer.- Good example:
is_active
,has_data
,total_amount
- Bad example:
flag
,value
,temp

3. Function Naming Conventions
Use snake_case for function names as well, and it’s best to use a verb to clearly convey its purpose. Starting the name with a verb makes the function’s role instantly recognizable. Good examples:fetch_user_data
, calculate_average
, process_order
Bad examples: getData
, Calculate_Average
, orderProcess
Tips for Naming with Verbs
Start function names with a verb so it’s clear what they do. For example, using “calculate_total” or “is_valid” makes the function’s purpose obvious. Avoid overly long names or redundant phrasing; aim for simple, easy-to-understand naming.4. Class Naming Conventions
Use CamelCase for class names. CamelCase capitalizes the first letter of each word and does not use underscores, helping apply a consistent format to class names. Good examples:UserProfile
, OrderProcessor
, DataManager
Bad examples: user_profile
, order_processor
, data_manager
Make the Class Purpose Clear
Since a class serves as a blueprint for objects, it’s important to give it a name that clearly conveys its role. For example, “UserManager” is a class that handles user management, and “FileHandler” is a class that performs file operations, making their purposes clear.5. Constant Naming Conventions
Constants are used to hold values that do not change and are written in all caps. Using snake_case with underscores between words makes it clear that they are constants. Good example:MAX_RETRIES
, BASE_URL
, TIMEOUT_LIMIT
Bad example: maxRetries
, baseurl
, TimeoutLimit
How to Make Constant Management Easier
Grouping related constants together in a class or file makes maintenance easier. For example, consolidating application configuration constants in aConfig
class simplifies referencing and managing them.6. Naming Conventions for Modules and Packages
Use short, clear lowercase words for module and package names. When possible, avoid underscores and name them so their role within the project is immediately obvious. Good examples:utils
, data_processing
, core
Bad examples: DataProcessing
, CoreUtilsPackage
, Helper_Functions
Following Python’s naming conventions for modules and packages keeps the entire project organized and makes it easier for other developers to navigate. Python doesn’t have access control features, but private variables and methods are indicated by prefixing them with an underscore (_). This signals that they are intended for internal use and conveys that purpose to other developers. : _internal_method
, _private_data
: internalMethod
, PrivateData
Prefixing a name with double underscores (__) triggers name mangling, which helps avoid variable name collisions within a class. This prevents unintended overriding in subclasses and is especially useful in large-scale class designs.8. Special Naming Conventions (Dunder Methods)
Python has special methods called “dunder methods” (double underscore methods) that are surrounded by double underscores at the beginning and end of their names. These are used to implement Python’s standard behavior and functionality. Example__init__
: Initialization method called when a class is instantiated__str__
: Returns the string representation of the object__len__
: Returns the length of the object

9. Importance of Naming Conventions and Best Practices
Following naming conventions is important for improving code readability and maintainability, and for boosting the efficiency of team development. Proper naming conventions not only make the code easier to understand, but also simplify future edits and bug fixes, contributing to overall project quality.Consistent Naming
Adopting a consistent naming convention makes it easier for other developers to understand the code, allowing reviews and revisions to proceed smoothly. When the naming style is uniform, variables and functions can be instantly identified by their role, making the code naturally clearer.Use Meaningful Names
Variable and function names should clearly express their role or purpose. For example,total_count
or is_valid
, where the name itself describes the role, allowsively understand what the variable or function represents. Avoid vague names like “temp” or “value,” and aim for specific, easy‑to‑understand names.Avoid Excessive Abbreviation or Decoration
Applying excessive abbreviations or decorative elements to names can actually make them harder to read. Following Python’s naming conventions and aiming for short yet clear names improves code readability.10. Tools and Tips Useful in Practice
To maintain naming conventions that follow PEP 8, automated formatting and static analysis tools are helpful. Below are tools you can use in real-world projects.- Black: An automatic formatter for Python code. It reformats code to comply with PEP 8, unifying indentation, spacing, and naming conventions. You can easily see before-and-after formatting, which greatly helps maintain a consistent style.
- Pylint: A static analysis tool that checks naming conventions, errors, and points out redundant code. Using Pylint makes it easy to spot style violations and bugs early according to PEP 8, improving code quality.
- Flake8: A tool for keeping code formatted according to style guides. When used together with Black and Pylint, it enables even finer style checks and verification of naming conventions.